Coder Social home page Coder Social logo

bops's Introduction

bops

Build Status TESTLING NPM

buffer/typed array agnostic buffer operations.

bops presents a JavaScript API for working with binary data that will work exactly the same in supported browsers and in node. due to the way that Buffer is implemented in node it is impossible to take code written against the Buffer API and make it work on top of binary data structures (Array Buffers and Typed Arrays) in the browser.

instead you have to fake the API on top of Object, but Object isn't designed for holding raw binary data and will be really slow/memory inefficient for many common binary use cases (parsing files, writing files, etc).

use bops in place of Buffer or Uint8Array to make your binary JS code fast + portable.

var binary = require('bops')

binary.readUInt8(new Buffer(10), 0) // === 10

in browser, will default to using Uint8Array instances. in node, will default to using Buffer instances.

API

supported encodings

  • utf8
  • hex
  • base64

bops.from(str, encoding="utf8") -> buf

given a string (and optional encoding) return a native buffer instance. also accepts arrays.

bops.to(buf, encoding="utf8") -> str

given a native buffer (and optional encoding) return a string.

bops.is(buf) -> bool

given a native buffer returns true. Returns false for other values.

bops.subarray(buf, start[, end]) -> buf

return a view onto the original buffer.

bops.join([buf, buf, buf]) -> buf

compile several buffers into a single buffer.

bops.create(size) -> buf

create a native buffer instance of size.

bops.copy(source, target, target_start, source_start, source_end)

perform a fast copy from one native buffer to another.

readUInt8(buf, at)

readInt8(buf, at)

readUInt16LE(buf, at)

readUInt32LE(buf, at)

readInt8(buf, at)

readInt16LE(buf, at)

readInt32LE(buf, at)

readFloatLE(buf, at)

readDoubleLE(buf, at)

readUInt16BE(buf, at)

readUInt32BE(buf, at)

readInt16BE(buf, at)

readInt32BE(buf, at)

readFloatBE(buf, at)

readDoubleBE(buf, at)

read a value from a buffer at a given byte offset.

writeUInt8(buf, value, at)

writeInt8(buf, value, at)

writeUInt16LE(buf, value, at)

writeUInt32LE(buf, value, at)

writeInt8(buf, value, at)

writeInt16LE(buf, value, at)

writeInt32LE(buf, value, at)

writeFloatLE(buf, value, at)

writeDoubleLE(buf, value, at)

writeUInt16BE(buf, value, at)

writeUInt32BE(buf, value, at)

writeInt16BE(buf, value, at)

writeInt32BE(buf, value, at)

writeFloatBE(buf, value, at)

writeDoubleBE(buf, value, at)

write a value to a buffer at a given byte offset.

License

MIT

bops's People

Contributors

chrisdickinson avatar creationix avatar max-mapper avatar mcollina avatar micburks avatar swashcap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bops's Issues

copy function

I think your copy function is one off.
if I try to copy

var buf = buffer.create(32);
buffer.copy( buffer.from( 'abcdefg' ), buf, 0, 0, 5 );
console.log( buffer.to( buffer.subarray( buf, 0, 5 ) ) );

it outputs 'abcde'
isn't it suppose to be 'abcdef' ?
because that's index 0 1 2 3 4 5

it's kinda strange that beginning ( index 0) is inclusive but last index (index 5) isn't

I kinda doubt this issue is gonna be resolved looking at last commit
I'll try to hack into the code and perhaps fork it somehow
:) I don't know how send fixes to repository that isn't mine just yet...

Bad conversion when used in browser

I'm experiencing a conversion issue when using Bops browser-side with chinese character strings.

Consider the following:

var hao = '好';
var haoToBuffer = bops.from(hao);
var haoBackToString = bops.to(haoToBuffer);
console.log(hao, haoBackToString, haoToBuffer);

This outputs 好 } [125]. is being mistranslated to }.

When running the same code in NodeJS, the output is correct:

好 好 <Buffer e5 a5 bd>

I can see the buffers being generated vary between NodeJS and browser (in this case, Google Chrome).

Anyone able to shed some light if I'm doing something incorrectly?

Use of deprecated API

Usage of new Buffer() is deprecated. source

This outputs a message when used and fails tests when --throw-deprecation flag is used, i.e.:

node --throw-deprecation test/index.js

Buffer.alloc and Buffer.from can be used instead

Firefox Support

I am getting an error in the https://github.com/chrisdickinson/bops/blob/master/typedarray/subarray.js#L4 file when running in firefox.

Just wanted to know if anyone had any ideas, I believe some people are using slice as an alternative but haven't dug into it to far.

TypeError: buf.subarray is not a function

In the firefox inspector it is clear that that Object pass in is a buffer, however it has no subarray method.

The project I am using this via is https://github.com/mcollina/mows

"Browser's" buffers instead of TypedArray

Hello!
I was having a problem when using bops (from within bytewise) on the browser.
The problem "seems" to have disappeared when I started using https://github.com/feross/buffer
I say seems, cause i'm having other problems.. so not sure if I'm facing a different issue or that using https://github.com/feross/buffer didn't fix anything to begin with,
I'm mentioning this here as I thought one might find this information useful.
As for myself, I still have some digging to do

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.