Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hazae41/binary

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hazae41/binary

Zero-copy binary data types

  • 1.2.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27
increased by22.73%
Maintainers
1
Weekly downloads
 
Created
Source
npm i @hazae41/binary

Node Package 📦

Features

Current features

  • 100% TypeScript and ESM
  • No external dependencies
  • Unit-tested
  • Zero-copy reading and writing

Usage

Cursor

Writing
const cursor = Cursor.allocUnsafe(1024)

cursor.writeUint8(123)
cursor.writeUint16(1234)

console.log(cursor.offset) // 3
Reading
const bytes = new Uint8Array(/*...*/)
const cursor = new Cursor(bytes)

const uint8 = cursor.readUint8()
const uint16 = cursor.readUint16()

console.log(cursor.offset) // 3

Binary data types

Writable
class MyObject implements Writable {

  constructor(
    readonly x: number,
    readonly y: number
  ) {}

  size() {
    return 1 + 2
  }

  write(cursor: Cursor) {
    cursor.writeUint8(this.x)
    cursor.writeUint16(this.y)
  }

}
const myobject = new MyObject(1, 515)
const bytes = Writable.toBytes(myobject) // Uint8Array([1, 2, 3])
Readable
class MyObject {

  constructor(
    readonly x: number,
    readonly y: number
  ) {}

  static read(cursor: Cursor) {
    const x = cursor.readUint8()
    const y = cursor.readUint16()

    return new this(x, y)
  }

}
const bytes = new Uint8Array([1, 2, 3])
const myobject = Readable.fromBytes(MyObject, bytes) // MyObject(1, 515)
Opaque

This is a binary data type that just holds bytes, it can be used when a binary data type is required

const bytes = new Uint8Array([1, 2, 3])
const opaque = Readable.fromBytes(SafeOpaque, bytes) // Opaque(Uint8Array([1, 2, 3]))
const myobject = opaque.into(MyObject) // MyObject(1, 515)
const myobject = new MyObject(1, 515)
const opaque = Opaque.from(myobject) // Opaque(Uint8Array([1, 2, 3]))
const bytes = Writable.toBytes(opaque) // Uint8Array([1, 2, 3])

Keywords

FAQs

Package last updated on 22 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc