A library for managing ArrayBufferLike structures including conversion between Base64 and Uint8Array, and buffer validation.
Go to file
2024-04-14 17:23:03 +02:00
.vscode fix(core): update 2022-06-15 23:36:51 +02:00
test fix(core): update 2024-02-29 22:43:37 +01:00
ts fix(core): update 2024-02-29 22:43:37 +01:00
.gitignore fix(core): update 2022-06-15 23:36:51 +02:00
.gitlab-ci.yml fix(core): update 2022-06-15 23:36:51 +02:00
license fix(core): update 2022-06-16 00:05:30 +02:00
npmextra.json update tsconfig 2024-04-14 17:23:03 +02:00
package.json update tsconfig 2024-04-14 17:23:03 +02:00
pnpm-lock.yaml fix(core): update 2024-02-25 01:33:15 +01:00
readme.hints.md update tsconfig 2024-04-14 17:23:03 +02:00
readme.md update tsconfig 2024-04-14 17:23:03 +02:00
tsconfig.json fix(core): update 2024-02-25 01:17:47 +01:00

@push.rocks/smartbuffer

handle ArrayBufferLike structures

Install

To install @push.rocks/smartbuffer, use the following command with npm:

npm install @push.rocks/smartbuffer --save

or with yarn:

yarn add @push.rocks/smartbuffer

This will add @push.rocks/smartbuffer to your project's dependencies and you'll be ready to use it in your application.

Usage

@push.rocks/smartbuffer is designed to make handling ArrayBufferLike structures easier and more intuitive, specifically in TypeScript and in environments that support ECMAScript modules. This includes web browsers, Node.js (with ESM support enabled), and Deno.

Basic Concepts

Before diving into the usage examples, lets clarify what ArrayBufferLike structures are. In JavaScript, the ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. An ArrayBufferLike is any data structure that can be viewed as representing such binary data. The Buffer class in Node.js and Uint8Array are typical examples of ArrayBufferLike structures.

Importing the Module

First, import the functions you need from the module:

import { arrayBufferToBase64, base64ToArrayBuffer, isBufferLike } from '@push.rocks/smartbuffer';

Converting ArrayBuffer to Base64 String

Often, youll need to convert binary data represented by an ArrayBuffer into a Base64 encoded string. This is especially common when you need to transmit binary data over a medium that only supports text content, such as JSON.

const myArrayBuffer = new ArrayBuffer(8);
const view = new Uint8Array(myArrayBuffer);
// Fill the buffer with some data
view.set([8, 6, 7, 5, 3, 0, 9]);

const base64String = arrayBufferToBase64(myArrayBuffer);
console.log(base64String); // Output will be a Base64 encoded string of the ArrayBuffer content

Converting a Base64 String to ArrayBuffer

Conversely, if you have a Base64 encoded string that represents binary data, you can convert it back to an ArrayBuffer or a Uint8Array for further manipulation or use within your application.

const base64String = "aGVsbG8gd29ybGQ="; // "hello world" in Base64
const arrayBuffer = base64ToArrayBuffer(base64String);
console.log(new TextDecoder().decode(arrayBuffer)); // Expects to output "hello world"

Checking if an Object is ArrayBufferLike

Sometimes you may need to validate whether a given object is an ArrayBufferLike structure (ArrayBuffer, Uint8Array, Buffer, etc.). This is useful in functions that are meant to operate on binary data and need to ensure the data passed to them is in the correct format.

const buffer = new Uint8Array([1, 2, 3, 4]);
const notBuffer = [1, 2, 3, 4];

console.log(isBufferLike(buffer)); // true
console.log(isBufferLike(notBuffer)); // false

Practical Applications

The functionality provided by @push.rocks/smartbuffer is particularly helpful in applications dealing with file operations, network communications, or any context where binary data needs to be processed, transformed, or transmitted.

For instance, when receiving files as part of a web API, converting these files to Base64 strings for storage or transmission is a common requirement. Similarly, when working with WebSockets or server-sent events, ArrayBuffer objects and their views (Uint8Array, etc.) are commonly used to handle binary data efficiently.

By providing utilities to convert between binary data and Base64 strings, as well as methods to check data types, @push.rocks/smartbuffer simplifies the implementation of complex data handling operations. Whether you are working with IoT devices, implementing a web application that manipulates images or audio files, or dealing with any form of binary data, @push.rocks/smartbuffer is a valuable tool to have in your toolkit.

Remember to thoroughly test your applications to ensure that data integrity is maintained throughout all operations involving ArrayBufferLike structures.


The provided examples cover the core features of @push.rocks/smartbuffer. For advanced use cases, refer to the module's documentation and explore the source code to better understand its capabilities and how it can be integrated into your specific projects.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.