JSPM

  • Created
  • Published
  • 0
  • Score
    100M100P100Q24867F
  • License MIT

A JavaScript byte array library for the browser, Deno and Node.js

Package Exports

  • @i-xi-dev/bytes

Readme

@i-xi-dev/bytes

A JavaScript byte array library for the browser, Deno and Node.js

Description

Conversion

Conversion

Requirement

Browser

...

Node.js

16.5.0+

Installation

npm

$ npm i @i-xi-dev/bytes
import { ByteSequence, Resource } from "@i-xi-dev/bytes";

CDN

import { ByteSequence, Resource } from "https://unpkg.com/@i-xi-dev/bytes";

Example

Creating an instance of ByteSequence class

Creating an instance that views the specified ArrayBuffer

const arrayBufferView = new ByteSequence(arrayBuffer);

Creating an instance with a specific size

const zeroFilledBytes = ByteSequence.create(size);

Creating an instance filled with random bytes

const randomBytes = ByteSequence.generateRandom(size);

Creating an instance with a new underlying buffer

const copiedBytes = ByteSequence.from(uint8Array);
const copiedBytes2 = ByteSequence.from(copiedBytes);

Creating an instance by isomorphic encoding the binary string

const isomorphicEncoded = ByteSequence.fromBinaryString("hello");
// [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]

Creating an instance by parsing the hexadecimal formatted

const parsed = ByteSequence.parse("68656c6c6f");
// [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]

Creating an instance by decoding the Base64 encoded

const decoded = ByteSequence.fromBase64("aGVsbG8=");
// [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]

Creating an instance by reading the ReadableStream of Uint8Array

const loadedBytes = await ByteSequence.fromByteStream(byteStream);

Node.js Readable stream of Buffer is also available.

const loadedBytes = await ByteSequence.fromByteStream(byteStream);

Creating an instance by encoding the text in UTF-8

const encoded = ByteSequence.fromText("hello");
// [ 0x68, 0x65, 0x6C, 0x6C, 0x6F ]

const encoded = ByteSequence.fromText("新幹線");
// [ 0xE6, 0x96, 0xB0, 0xE5, 0xB9, 0xB9, 0xE7, 0xB7, 0x9A ]

Creating an instance by reading the Blob

const resource = await Resource.fromBlob(blob);
const loadedBytes = resource.bytes;

Creating an instance by reading the data URL

const resource = await Resource.fromDataUrl("data:text/plain;charset=US-ASCII,hello");
const loadedBytes = resource.bytes;

Converting the instance to a Base64 encoded string

const bytes = ByteSequence.fromBinaryString("hello");
const base64Encoded = bytes.toBase64();
// "aGVsbG8="

...