Code Monkey home page Code Monkey logo

bufio's Introduction

bufio

Buffer and serialization utilities for javascript.

Usage

const assert = require('assert');
const bio = require('bufio');

const bw = bio.write();
bw.writeU64(100);
bw.writeString('foo');
const data = bw.render();

const br = bio.read(data);
assert(br.readU64() === 100);
assert(br.readString(3) === 'foo');

Struct Usage

const bio = require('bufio');

class MyStruct extends bio.Struct {
  constructor() {
    super();
    this.str = 'hello';
    this.value = 0;
  }

  write(bw) {
    bw.writeVarString(this.str, 'ascii');
    bw.writeU64(this.value);
    return this;
  }

  read(br) {
    this.str = br.readVarString('ascii');
    this.value = br.readU64();
    return this;
  }
}

const obj = new MyStruct();

console.log('Buffer:');
console.log(obj.encode());

console.log('Decoded:');
console.log(MyStruct.decode(obj.encode()));

console.log('Hex:');
console.log(obj.toHex());

console.log('Decoded:');
console.log(MyStruct.fromHex(obj.toHex()));

console.log('Base64:');
console.log(obj.toBase64());

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

  • Copyright (c) 2017, Christopher Jeffrey (MIT License).

See LICENSE for more info.

bufio's People

Contributors

chjj avatar danield9tqh avatar nodech avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bufio's Issues

SEEKING INPUT: Default string encoding causes many many bugs

Ok, I want this to be more of a conversational piece that I'm looking for input but we're seeing a design decision in bufio cause a lot of errors crop up in our code base.

Let's examine the following code sample pretty much taken from the documentation

var bio = require("bufio")

const bw = bio.write();
bw.writeVarString('💪💎🚀', 'binary')
const bytes = bw.render()

const br = bio.read(bytes);
console.log(br.readVarString('binary'))

> Output: "=ª=�=�"

The output is a string that doesn't represent the original input. The reasoning is obvious. Binary encoding is not binary encoding, it's actually an alias for latin-1 which is affectively ASCII.

'binary': Alias for 'latin1'. See binary strings for more background on this topic. The name of this encoding can be very misleading, as all of the encodings listed here convert between strings and binary data. For converting between strings and Buffers, typically 'utf8' is the right choice.

'ascii': For 7-bit ASCII data only. When encoding a string into a Buffer, this is equivalent to using 'latin1'. When decoding a Buffer into a string, using this encoding will additionally unset the highest bit of each byte before decoding as 'latin1'. Generally, there should be no reason to use this encoding, as 'utf8' (or, if the data is known to always be ASCII-only, 'latin1') will be a better choice when encoding or decoding ASCII-only text. It is only provided for legacy compatibility.

See https://nodejs.org/api/buffer.html#buffers-and-character-encodings

This is problematic because bufios default encoding is binary by default for varStrings. Most user input, and most input across the internet now is expected to be UTF8. Developers on our team who use bufio naturally don't realize they should be passing a second encoding as 'utf8' in the encoding parameter of writeVarString. The reason is because node in general defaults to utf8 everywhere.

Is there some reason for this design decision? Particularly because ASCII is encoded as single byte characters by default in UTF8? I think changing the default encoding would be a very hairy decision that could break backwards compatibility, but I also don't think anyone using bufio should ever encode strings as ascii. 99% of the time you are creating a bug in your platform.

We may change our typescript type definitions to make encoding not optional, which would force developers to pass it in at the cost of diverging from bufios actual code interface.

https://github.com/iron-fish/ironfish/blob/master/ironfish/src/typedefs/bufio.d.ts#L50

I'll close this afterwards since it's an open ended conversation.

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.