inolen / bit-buffer Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
If the BitView constructor is invoked with (non-zero) byteOffset and no byteLength, it fails with Uncaught RangeError: Invalid typed array length
error.
To replicate:
$ node
> BitView = require('bit-buffer').BitView;
> array = new ArrayBuffer(8);
> bv = new BitView(array, 4);
Uncaught RangeError: Invalid typed array length: 8
at new Uint8Array (<anonymous>)
at new BitView (.../node_modules/bit-buffer/bit-buffer.js:22:15)
I think the solution would be to change line 20 to
byteLength = byteLength || source.byteLength-byteOffset /* ArrayBuffer */ || source.length-byteOffset /* Buffer */;
It would be useful to have a fseek function with seek_cur and seek_set modes. Is bb.byteIndex = newOffset; // seek_set
and bb.byteIndex += offset; // seek_cur
equivalent here?
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 5
Cannot find name 'Buffer'.
L4: export class BitStream {
L5: constructor(source: ArrayBuffer | Buffer, byteOffset?: number, byteLength?: number)
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 10
Cannot find name 'Buffer'.
L9: bitsLeft: number;
L10: buffer: Buffer;
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 15
L11: index: number;
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 35
'writeBits', which lacks return-type annotation, implicitly has an 'any' return type.
L15: writeBits(value: number, bits: number);
'writeBoolean', which lacks return-type annotation, implicitly has an 'any' return type.
L35: writeBoolean(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 37
'writeInt8', which lacks return-type annotation, implicitly has an 'any' return type.
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 39
L37: writeInt8(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 41
'writeUint8', which lacks return-type annotation, implicitly has an 'any' return type.
L39: writeUint8(value: number);
'writeInt16', which lacks return-type annotation, implicitly has an 'any' return type.
L41: writeInt16(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 43
'writeUint16', which lacks return-type annotation, implicitly has an 'any' return type.
L43: writeUint16(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 45
'writeInt32', which lacks return-type annotation, implicitly has an 'any' return type.
L45: writeInt32(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 47
'writeUint32', which lacks return-type annotation, implicitly has an 'any' return type.
L47: writeUint32(value: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 49
'writeFloat32', which lacks return-type annotation, implicitly has an 'any' return type.
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 51
L49: writeFloat32(value: number);
'writeFloat64', which lacks return-type annotation, implicitly has an 'any' return type.
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 57
L51: writeFloat64(value: number);
'writeASCIIString', which lacks return-type annotation, implicitly has an 'any' return type.
L57: writeASCIIString(data: string, length?: number);
[23:52:56] typescript: node_modules/bit-buffer/bit-buffer.d.ts, line: 59
'writeUTF8String', which lacks return-type annotation, implicitly has an 'any' return type.
L59: writeUTF8String(data: string, length?: number);
[23:52:56] transpile failed
[23:52:56] ionic-app-script task: "build"
[23:52:56] Error: Error
This project is under the MIT license yet no LICENSE file is present. Providing one will be useful in including the notice in things that use this,
_
new Buffer()
is deprecated, consider using Buffer.from() instead.new Buffer()
(which is, once again, deprecated) with anything other than an array-like object returns an error.Buffer.from(this._view.buffer)
or just return this._view.buffer
as calling BitView's buffer property getter creates a new Buffer instance every time.The changes introduced in the pull requests that have been merged since 0.0.3 seem to have improved performance by over 15% in my code.
I'm wondering whether a new version of the library could be released so I can make use of these changes.
Thanks
Would be really useful
I might be misunderstanding how this library works, but I'm not seeing the results I would expect when reading in 8 single bits.
bitBuffer = require('bit-buffer');
// read in an "H" bit by bit
b = Buffer.from('H');
bv = new bitBuffer.BitView(b);
bv.bigEndian = false;
arr = [];
for (let i = 0; i < 8; i++) {
arr.push(bv.getBits(i, 1));
}
// store some results
arrReversed = [...arr].reverse();
realResult = 'H'.charCodeAt(0).toString(2).padStart(8, '0');
bitViewResult = arrReversed.join('');
// convert back to chars
realResultAsChar = String.fromCharCode(parseInt(realResult, 2));
bitViewResultAsChar = String.fromCharCode(parseInt(bitViewResult, 2));
// print result
console.log({
realResult,
bitViewResult,
realResultAsChar,
bitViewResultAsChar,
arr,
arrReversed,
});
The above code outputs:
{
realResult: '01001000',
bitViewResult: '00101111',
realResultAsChar: 'H',
bitViewResultAsChar: '/',
arr: [
1, 1, 1, 1,
0, 1, 0, 0
],
arrReversed: [
0, 0, 1, 0,
1, 1, 1, 1
]
}
I would expect the bitViewResult
to match the realResult
and I would expect bitViewResultAsChar
to be 'H'
.
I don't understand why the first 4 bits it reads in are 1
.
Thanks anyways for sharing your work!
node v0.10 has changed the behaviour when creating a Uint8Array with a Buffer. It no longer shares the reference, but clones the buffer. That means any changes made with writeBits is not reflect in the buffer property.
It'd be useful in some use cases to have a skipBits function, which just advances the offset without reading any bits - if you know you don't care about the value of some bits, you shouldn't have to read them and compute their value.
npm package is older than GitHub version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.