smartbuffer/readme.md

106 lines
5.6 KiB
Markdown
Raw Normal View History

2024-04-14 15:23:03 +00:00
# @push.rocks/smartbuffer
2022-06-15 21:36:51 +00:00
handle ArrayBufferLike structures
2024-04-14 15:23:03 +00:00
## Install
To install `@push.rocks/smartbuffer`, use the following command with npm:
```sh
npm install @push.rocks/smartbuffer --save
```
or with yarn:
```sh
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.
2022-06-15 21:36:51 +00:00
## Usage
2024-04-14 15:23:03 +00:00
`@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:
```typescript
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.
```typescript
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.
```typescript
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.
```typescript
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.
## License and Legal Information
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](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
2022-06-15 21:36:51 +00:00
2024-04-14 15:23:03 +00:00
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.
2022-06-15 21:36:51 +00:00
2024-04-14 15:23:03 +00:00
### Company Information
2022-06-15 21:36:51 +00:00
2024-04-14 15:23:03 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2022-06-15 21:36:51 +00:00
2024-04-14 15:23:03 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2022-06-15 21:36:51 +00:00
2024-04-14 15:23:03 +00:00
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.