Coder Social home page Coder Social logo

erickj / bytebuffer.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from protobufjs/bytebuffer.js

0.0 2.0 0.0 758 KB

A full-featured ByteBuffer implementation in JavaScript.

Home Page: http://dcode.io

License: Apache License 2.0

JavaScript 98.26% CSS 1.74%

bytebuffer.js's Introduction

ByteBuffer.js - A full-featured ByteBuffer implementation in JavaScript

Provides a full-featured ByteBuffer implementation using typed arrays. It's one of the core components driving ProtoBuf.js and the PSON reference implementation.

Note: The API behind #toHex and #toString has changed with ByteBuffer 2, which is a generally revised release, in favor of making this more intuitive.

What can it do?

  • Mimics Java ByteBuffers as close as reasonable while using typed array terms
  • Signed and unsigned integers (8, 16, 32, 64 bit through Long.js) with endianness support
  • 32 and 64 bit floats
  • Varints as known from protobuf including zig-zag encoding
  • Includes an UTF8 and Base64 en-/decoder
  • C-strings, V(arint-prefixed)-strings and UTF8 L(ength-prefixed)-strings
  • Rich string toolset (to hex, base64, binary, utf8, debug, columns)
  • Relative and absolute zero-copy operations
  • Manual and automatic resizing (efficiently doubles capacity)
  • Chaining of all operations that do not return a specific value
  • Slicing, appending, prepending, reversing, flip, mark, reset, etc.

And much more...

Features

  • CommonJS compatible
  • RequireJS/AMD compatible
  • node.js compatible, also available via npm
  • Browser compatible
  • Closure Compiler ADVANCED_OPTIMIZATIONS compatible (fully annotated, ByteBuffer.min.js has been compiled this way, ByteBuffer.min.map is the source map)
  • Fully documented using jsdoc3
  • Well tested through nodeunit
  • Zero production dependencies (Long.js is optional)
  • Small footprint

Usage

Node.js / CommonJS

  • Install: npm install bytebuffer
var ByteBuffer = require("bytebuffer");
var bb = new ByteBuffer();
bb.writeLString("Hello world!").flip();
console.log(bb.readLString()+" from ByteBuffer.js");

Browser

Optionally depends on Long.js for long (int64) support. If you do not require long support, you can skip the Long.js include.

<script src="Long.min.js"></script>
<script src="ByteBuffer.min.js"></script>
var ByteBuffer = dcodeIO.ByteBuffer;
var bb = new ByteBuffer();
bb.writeLString("Hello world!").flip();
alert(bb.readLString()+" from ByteBuffer.js");

Require.js / AMD

Optionally depends on Long.js for long (int64) support. If you do not require long support, you can skip the Long.js config. Require.js example:

require.config({
    "paths": {
        "Long": "/path/to/Long.js"
        "ByteBuffer": "/path/to/ByteBuffer.js"
    }
});
require(["ByteBuffer"], function(ByteBuffer) {
    var bb = new ByteBuffer();
    bb.writeLString("Hello world!");
    bb.flip();
    alert(bb.readLString()+" from ByteBuffer.js");
});

On long (int64) support

As of the ECMAScript specification, number types have a maximum value of 2^53. Beyond that, behaviour might be unexpected. However, real long support requires the full 64 bits with the possibility to perform bitwise operations on the value for varint en-/decoding. So, to enable true long support in ByteBuffer.js, it optionally depends on Long.js, which actually utilizes two 32 bit numbers internally. If you do not require long support at all, you can skip it and save the additional bandwidth. On node, long support is available by default through the long dependency.

Downloads

Documentation

Tests (& Examples) Build Status

Support for IE<10, FF<15, Chrome<9 etc.

  • Requires working ArrayBuffer & DataView implementations (i.e. use a polyfill)

Contributors

Dretch (IE8 compatibility)

License

Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html

bytebuffer.js's People

Contributors

dcodeio avatar dretch avatar adambom avatar

Watchers

Erick J avatar James Cloos avatar

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.