Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,1 @@ | ||
{"processes":{"cee0cfa2-f287-4fe8-85f6-acacb6069c06":{"parent":null,"children":[]}},"files":{},"externalIds":{}} | ||
{"processes":{"284f1aaf-b3d6-497f-9a6f-42b65aadeb37":{"parent":null,"children":[]}},"files":{},"externalIds":{}} |
@@ -10,3 +10,3 @@ { | ||
"request": "launch", | ||
"name": "DEBUG in Chrome", | ||
"name": "DEBUG lib0 in Chrome", | ||
"url": "http://localhost:3443", | ||
@@ -13,0 +13,0 @@ "webRoot": "${workspaceFolder}" |
@@ -70,1 +70,13 @@ import * as string from './string.js' | ||
export const fromBase64 = env.isBrowser ? fromBase64Browser : fromBase64Node | ||
/** | ||
* Copy the content of an Uint8Array view to a new ArrayBuffer. | ||
* | ||
* @param {Uint8Array} uint8Array | ||
* @return {Uint8Array} | ||
*/ | ||
export const copyUint8Array = uint8Array => { | ||
const newBuf = createUint8ArrayFromLen(uint8Array.byteLength) | ||
newBuf.set(uint8Array) | ||
return newBuf | ||
} |
@@ -7,10 +7,10 @@ /** | ||
/** | ||
* A Decoder handles the decoding of an ArrayBuffer. | ||
* A Decoder handles the decoding of an Uint8Array. | ||
*/ | ||
export class Decoder { | ||
/** | ||
* @param {ArrayBuffer} buffer Binary data to decode | ||
* @param {Uint8Array} uint8Array Binary data to decode | ||
*/ | ||
constructor (buffer) { | ||
this.arr = new Uint8Array(buffer) | ||
constructor (uint8Array) { | ||
this.arr = uint8Array | ||
this.pos = 0 | ||
@@ -22,6 +22,6 @@ } | ||
* @function | ||
* @param {ArrayBuffer} buffer | ||
* @param {Uint8Array} uint8Array | ||
* @return {Decoder} | ||
*/ | ||
export const createDecoder = buffer => new Decoder(buffer) | ||
export const createDecoder = uint8Array => new Decoder(uint8Array) | ||
@@ -45,3 +45,3 @@ /** | ||
export const clone = (decoder, newPos = decoder.pos) => { | ||
const _decoder = createDecoder(decoder.arr.buffer) | ||
const _decoder = createDecoder(decoder.arr) | ||
_decoder.pos = newPos | ||
@@ -52,23 +52,29 @@ return _decoder | ||
/** | ||
* Read `len` bytes as an ArrayBuffer. | ||
* Create an Uint8Array view of the next `len` bytes and advance the position by `len`. | ||
* | ||
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks. | ||
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array. | ||
* | ||
* @function | ||
* @param {Decoder} decoder The decoder instance | ||
* @param {number} len The length of bytes to read | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
export const readArrayBuffer = (decoder, len) => { | ||
const arrayBuffer = buffer.createUint8ArrayFromLen(len) | ||
export const readUint8Array = (decoder, len) => { | ||
const view = buffer.createUint8ArrayViewFromArrayBuffer(decoder.arr.buffer, decoder.pos, len) | ||
arrayBuffer.set(view) | ||
decoder.pos += len | ||
return arrayBuffer.buffer | ||
return view | ||
} | ||
/** | ||
* Read variable length payload as ArrayBuffer | ||
* Read variable length Uint8Array. | ||
* | ||
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks. | ||
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array. | ||
* | ||
* @function | ||
* @param {Decoder} decoder | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
export const readPayload = decoder => readArrayBuffer(decoder, readVarUint(decoder)) | ||
export const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder)) | ||
@@ -79,5 +85,5 @@ /** | ||
* @param {Decoder} decoder | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
export const readTail = decoder => readArrayBuffer(decoder, decoder.arr.length - decoder.pos) | ||
export const readTailAsUint8Array = decoder => readUint8Array(decoder, decoder.arr.length - decoder.pos) | ||
@@ -84,0 +90,0 @@ /** |
@@ -76,2 +76,15 @@ 'use strict'; | ||
/** | ||
* Copy the content of an Uint8Array view to a new ArrayBuffer. | ||
* | ||
* @param {Uint8Array} uint8Array | ||
* @return {Uint8Array} | ||
*/ | ||
const copyUint8Array = uint8Array => { | ||
const newBuf = createUint8ArrayFromLen(uint8Array.byteLength); | ||
newBuf.set(uint8Array); | ||
return newBuf | ||
}; | ||
exports.copyUint8Array = copyUint8Array; | ||
exports.createUint8ArrayFromArrayBuffer = createUint8ArrayFromArrayBuffer; | ||
@@ -78,0 +91,0 @@ exports.createUint8ArrayFromLen = createUint8ArrayFromLen; |
@@ -15,10 +15,10 @@ 'use strict'; | ||
/** | ||
* A Decoder handles the decoding of an ArrayBuffer. | ||
* A Decoder handles the decoding of an Uint8Array. | ||
*/ | ||
class Decoder { | ||
/** | ||
* @param {ArrayBuffer} buffer Binary data to decode | ||
* @param {Uint8Array} uint8Array Binary data to decode | ||
*/ | ||
constructor (buffer) { | ||
this.arr = new Uint8Array(buffer); | ||
constructor (uint8Array) { | ||
this.arr = uint8Array; | ||
this.pos = 0; | ||
@@ -30,6 +30,6 @@ } | ||
* @function | ||
* @param {ArrayBuffer} buffer | ||
* @param {Uint8Array} uint8Array | ||
* @return {Decoder} | ||
*/ | ||
const createDecoder = buffer => new Decoder(buffer); | ||
const createDecoder = uint8Array => new Decoder(uint8Array); | ||
@@ -53,3 +53,3 @@ /** | ||
const clone = (decoder, newPos = decoder.pos) => { | ||
const _decoder = createDecoder(decoder.arr.buffer); | ||
const _decoder = createDecoder(decoder.arr); | ||
_decoder.pos = newPos; | ||
@@ -60,23 +60,29 @@ return _decoder | ||
/** | ||
* Read `len` bytes as an ArrayBuffer. | ||
* Create an Uint8Array view of the next `len` bytes and advance the position by `len`. | ||
* | ||
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks. | ||
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array. | ||
* | ||
* @function | ||
* @param {Decoder} decoder The decoder instance | ||
* @param {number} len The length of bytes to read | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
const readArrayBuffer = (decoder, len) => { | ||
const arrayBuffer = buffer.createUint8ArrayFromLen(len); | ||
const readUint8Array = (decoder, len) => { | ||
const view = buffer.createUint8ArrayViewFromArrayBuffer(decoder.arr.buffer, decoder.pos, len); | ||
arrayBuffer.set(view); | ||
decoder.pos += len; | ||
return arrayBuffer.buffer | ||
return view | ||
}; | ||
/** | ||
* Read variable length payload as ArrayBuffer | ||
* Read variable length Uint8Array. | ||
* | ||
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks. | ||
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array. | ||
* | ||
* @function | ||
* @param {Decoder} decoder | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
const readPayload = decoder => readArrayBuffer(decoder, readVarUint(decoder)); | ||
const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder)); | ||
@@ -87,5 +93,5 @@ /** | ||
* @param {Decoder} decoder | ||
* @return {ArrayBuffer} | ||
* @return {Uint8Array} | ||
*/ | ||
const readTail = decoder => readArrayBuffer(decoder, decoder.arr.length - decoder.pos); | ||
const readTailAsUint8Array = decoder => readUint8Array(decoder, decoder.arr.length - decoder.pos); | ||
@@ -269,11 +275,11 @@ /** | ||
exports.peekVarUint = peekVarUint; | ||
exports.readArrayBuffer = readArrayBuffer; | ||
exports.readPayload = readPayload; | ||
exports.readTail = readTail; | ||
exports.readTailAsUint8Array = readTailAsUint8Array; | ||
exports.readUint16 = readUint16; | ||
exports.readUint32 = readUint32; | ||
exports.readUint8 = readUint8; | ||
exports.readUint8Array = readUint8Array; | ||
exports.readVarString = readVarString; | ||
exports.readVarUint = readVarUint; | ||
exports.readVarUint8Array = readVarUint8Array; | ||
exports.skip8 = skip8; | ||
//# sourceMappingURL=decoding.js.map |
@@ -21,3 +21,3 @@ 'use strict'; | ||
/** | ||
* A BinaryEncoder handles the encoding to an ArrayBuffer. | ||
* A BinaryEncoder handles the encoding to an Uint8Array. | ||
*/ | ||
@@ -57,9 +57,9 @@ class Encoder { | ||
/** | ||
* Transform to ArrayBuffer. TODO: rename to .toArrayBuffer | ||
* Transform to Uint8Array. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @return {ArrayBuffer} The created ArrayBuffer. | ||
* @return {Uint8Array} The created ArrayBuffer. | ||
*/ | ||
const toArrayBuffer = encoder => { | ||
const toUint8Array = encoder => { | ||
const uint8arr = new Uint8Array(length(encoder)); | ||
@@ -73,3 +73,3 @@ let curPos = 0; | ||
uint8arr.set(buffer.createUint8ArrayViewFromArrayBuffer(encoder.cbuf.buffer, 0, encoder.cpos), curPos); | ||
return uint8arr.buffer | ||
return uint8arr | ||
}; | ||
@@ -235,16 +235,16 @@ | ||
*/ | ||
const writeBinaryEncoder = (encoder, append) => writeArrayBuffer(encoder, toArrayBuffer(append)); | ||
const writeBinaryEncoder = (encoder, append) => writeUint8Array(encoder, toUint8Array(append)); | ||
/** | ||
* Append an arrayBuffer to the encoder. | ||
* Append fixed-length Uint8Array to the encoder. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @param {ArrayBuffer} arrayBuffer | ||
* @param {Uint8Array} uint8Array | ||
*/ | ||
const writeArrayBuffer = (encoder, arrayBuffer) => { | ||
const writeUint8Array = (encoder, uint8Array) => { | ||
const prevBufferLen = encoder.cbuf.length; | ||
// TODO: Append to cbuf if possible | ||
encoder.bufs.push(buffer.createUint8ArrayViewFromArrayBuffer(encoder.cbuf.buffer, 0, encoder.cpos)); | ||
encoder.bufs.push(buffer.createUint8ArrayFromArrayBuffer(arrayBuffer)); | ||
encoder.bufs.push(uint8Array); | ||
encoder.cbuf = buffer.createUint8ArrayFromLen(prevBufferLen); | ||
@@ -255,9 +255,11 @@ encoder.cpos = 0; | ||
/** | ||
* Append an Uint8Array to Encoder. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @param {ArrayBuffer} arrayBuffer | ||
* @param {Uint8Array} uint8Array | ||
*/ | ||
const writePayload = (encoder, arrayBuffer) => { | ||
writeVarUint(encoder, arrayBuffer.byteLength); | ||
writeArrayBuffer(encoder, arrayBuffer); | ||
const writeVarUint8Array = (encoder, uint8Array) => { | ||
writeVarUint(encoder, uint8Array.byteLength); | ||
writeUint8Array(encoder, uint8Array); | ||
}; | ||
@@ -272,12 +274,12 @@ | ||
exports.setUint8 = setUint8; | ||
exports.toArrayBuffer = toArrayBuffer; | ||
exports.toUint8Array = toUint8Array; | ||
exports.write = write; | ||
exports.writeArrayBuffer = writeArrayBuffer; | ||
exports.writeBinaryEncoder = writeBinaryEncoder; | ||
exports.writePayload = writePayload; | ||
exports.writeUint16 = writeUint16; | ||
exports.writeUint32 = writeUint32; | ||
exports.writeUint8 = writeUint8; | ||
exports.writeUint8Array = writeUint8Array; | ||
exports.writeVarString = writeVarString; | ||
exports.writeVarUint = writeVarUint; | ||
exports.writeVarUint8Array = writeVarUint8Array; | ||
//# sourceMappingURL=encoding.js.map |
@@ -341,8 +341,14 @@ 'use strict'; | ||
* @param {number} len | ||
* @return {ArrayBuffer} | ||
* @return {Uint16Array} | ||
*/ | ||
const arrayBuffer = (gen, len) => uint8Array(gen, len).buffer; | ||
const uint16Array = (gen, len) => new Uint16Array(uint8Array(gen, len * 2)); | ||
/** | ||
* @param {PRNG} gen | ||
* @param {number} len | ||
* @return {Uint32Array} | ||
*/ | ||
const uint32Array = (gen, len) => new Uint32Array(uint8Array(gen, len * 4)); | ||
exports.DefaultPRNG = DefaultPRNG; | ||
exports.arrayBuffer = arrayBuffer; | ||
exports.bool = bool; | ||
@@ -357,3 +363,5 @@ exports.char = char; | ||
exports.real53 = real53; | ||
exports.uint16Array = uint16Array; | ||
exports.uint32 = uint32; | ||
exports.uint32Array = uint32Array; | ||
exports.uint53 = uint53; | ||
@@ -360,0 +368,0 @@ exports.uint8Array = uint8Array; |
@@ -283,6 +283,5 @@ 'use strict'; | ||
switch (a.constructor) { | ||
case ArrayBuffer: { | ||
case ArrayBuffer: | ||
a = new Uint8Array(a); | ||
b = new Uint8Array(b); | ||
} | ||
// eslint-disable-next-line no-fallthrough | ||
@@ -289,0 +288,0 @@ case Uint8Array: { |
@@ -13,3 +13,3 @@ /** | ||
/** | ||
* A BinaryEncoder handles the encoding to an ArrayBuffer. | ||
* A BinaryEncoder handles the encoding to an Uint8Array. | ||
*/ | ||
@@ -49,9 +49,9 @@ export class Encoder { | ||
/** | ||
* Transform to ArrayBuffer. TODO: rename to .toArrayBuffer | ||
* Transform to Uint8Array. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @return {ArrayBuffer} The created ArrayBuffer. | ||
* @return {Uint8Array} The created ArrayBuffer. | ||
*/ | ||
export const toArrayBuffer = encoder => { | ||
export const toUint8Array = encoder => { | ||
const uint8arr = new Uint8Array(length(encoder)) | ||
@@ -65,3 +65,3 @@ let curPos = 0 | ||
uint8arr.set(buffer.createUint8ArrayViewFromArrayBuffer(encoder.cbuf.buffer, 0, encoder.cpos), curPos) | ||
return uint8arr.buffer | ||
return uint8arr | ||
} | ||
@@ -227,16 +227,16 @@ | ||
*/ | ||
export const writeBinaryEncoder = (encoder, append) => writeArrayBuffer(encoder, toArrayBuffer(append)) | ||
export const writeBinaryEncoder = (encoder, append) => writeUint8Array(encoder, toUint8Array(append)) | ||
/** | ||
* Append an arrayBuffer to the encoder. | ||
* Append fixed-length Uint8Array to the encoder. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @param {ArrayBuffer} arrayBuffer | ||
* @param {Uint8Array} uint8Array | ||
*/ | ||
export const writeArrayBuffer = (encoder, arrayBuffer) => { | ||
export const writeUint8Array = (encoder, uint8Array) => { | ||
const prevBufferLen = encoder.cbuf.length | ||
// TODO: Append to cbuf if possible | ||
encoder.bufs.push(buffer.createUint8ArrayViewFromArrayBuffer(encoder.cbuf.buffer, 0, encoder.cpos)) | ||
encoder.bufs.push(buffer.createUint8ArrayFromArrayBuffer(arrayBuffer)) | ||
encoder.bufs.push(uint8Array) | ||
encoder.cbuf = buffer.createUint8ArrayFromLen(prevBufferLen) | ||
@@ -247,9 +247,11 @@ encoder.cpos = 0 | ||
/** | ||
* Append an Uint8Array to Encoder. | ||
* | ||
* @function | ||
* @param {Encoder} encoder | ||
* @param {ArrayBuffer} arrayBuffer | ||
* @param {Uint8Array} uint8Array | ||
*/ | ||
export const writePayload = (encoder, arrayBuffer) => { | ||
writeVarUint(encoder, arrayBuffer.byteLength) | ||
writeArrayBuffer(encoder, arrayBuffer) | ||
export const writeVarUint8Array = (encoder, uint8Array) => { | ||
writeVarUint(encoder, uint8Array.byteLength) | ||
writeUint8Array(encoder, uint8Array) | ||
} |
{ | ||
"name": "lib0", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "", | ||
@@ -18,3 +18,3 @@ "dependencies": {}, | ||
"debug": "live-server --port=3443 --entry-file=test.html", | ||
"test": "npm run dist && npm run lint && nyc --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node ./dist/test.js --repitition-time 50", | ||
"test": "npm run dist && npm run lint && PRODUCTION=1 nyc --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node ./dist/test.js --repitition-time 50", | ||
"test-extensive": "npm test -- --repitition-time 30000", | ||
@@ -21,0 +21,0 @@ "test-code-coverage": "npm run dist && nyc --reporter html node ./dist/test.js", |
11
prng.js
@@ -195,4 +195,11 @@ /** | ||
* @param {number} len | ||
* @return {ArrayBuffer} | ||
* @return {Uint16Array} | ||
*/ | ||
export const arrayBuffer = (gen, len) => uint8Array(gen, len).buffer | ||
export const uint16Array = (gen, len) => new Uint16Array(uint8Array(gen, len * 2)) | ||
/** | ||
* @param {PRNG} gen | ||
* @param {number} len | ||
* @return {Uint32Array} | ||
*/ | ||
export const uint32Array = (gen, len) => new Uint32Array(uint8Array(gen, len * 4)) |
@@ -274,6 +274,5 @@ /** | ||
switch (a.constructor) { | ||
case ArrayBuffer: { | ||
case ArrayBuffer: | ||
a = new Uint8Array(a) | ||
b = new Uint8Array(b) | ||
} | ||
// eslint-disable-next-line no-fallthrough | ||
@@ -280,0 +279,0 @@ case Uint8Array: { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
753616
10985