Compare commits

...

23 Commits

Author SHA1 Message Date
bb89564339 update description 2024-05-29 14:11:56 +02:00
f480be9917 3.0.4 2024-04-25 18:08:50 +02:00
11bf0d9dbb fix(core): update 2024-04-25 18:08:49 +02:00
c58cc2b0df 3.0.3 2024-04-25 18:06:05 +02:00
87273cbbbd fix(core): update 2024-04-25 18:06:04 +02:00
bebc8b0ede 3.0.2 2024-04-17 20:34:18 +02:00
46e5217fc0 fix(core): update 2024-04-17 20:34:17 +02:00
ebb539e824 3.0.1 2024-04-17 20:33:24 +02:00
63e3b492e6 fix(core): update 2024-04-17 20:33:24 +02:00
158614c62c 3.0.0 2024-04-17 20:30:33 +02:00
f11114f6dc BREAKING CHANGE(core): update 2024-04-17 20:30:32 +02:00
f233cb1dcd 2.0.3 2024-04-17 19:49:30 +02:00
866da6f21a fix(core): update 2024-04-17 19:49:30 +02:00
30ac0ddbb4 2.0.2 2024-04-17 19:49:14 +02:00
4c5105aae7 fix(core): update 2024-04-17 19:49:14 +02:00
1f6e257cb7 2.0.1 2024-04-17 19:10:22 +02:00
0a79087c59 fix(core): update 2024-04-17 19:10:21 +02:00
432a59bcec 2.0.0 2024-04-17 19:09:56 +02:00
10d7c5d8e8 BREAKING CHANGE(core): switch to uInt8Extras 2024-04-17 19:09:55 +02:00
30306f3a40 update tsconfig 2024-04-14 17:23:03 +02:00
9a4859f6a0 update npmextra.json: githost 2024-04-01 21:33:58 +02:00
099a68d19f update npmextra.json: githost 2024-04-01 19:57:44 +02:00
78576b363e update npmextra.json: githost 2024-03-30 21:46:41 +01:00
8 changed files with 204 additions and 97 deletions

View File

@ -2,17 +2,32 @@
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartbuffer",
"description": "handle ArrayBufferLike structures",
"description": "A library for managing ArrayBufferLike structures including conversion between Base64 and Uint8Array, and buffer validation.",
"npmPackagename": "@push.rocks/smartbuffer",
"license": "MIT",
"projectDomain": "push.rocks"
"projectDomain": "push.rocks",
"keywords": [
"ArrayBuffer",
"TypeScript",
"base64 conversion",
"Uint8Array",
"data transformation",
"binary data handling",
"buffer validation",
"modular programming",
"ESM",
"utilities"
]
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
}
}

View File

@ -1,8 +1,8 @@
{
"name": "@push.rocks/smartbuffer",
"version": "1.0.7",
"version": "3.0.4",
"private": false,
"description": "handle ArrayBufferLike structures",
"description": "A library for managing ArrayBufferLike structures including conversion between Base64 and Uint8Array, and buffer validation.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
@ -34,5 +34,25 @@
"cli.js",
"npmextra.json",
"readme.md"
]
}
],
"keywords": [
"ArrayBuffer",
"TypeScript",
"base64 conversion",
"Uint8Array",
"data transformation",
"binary data handling",
"buffer validation",
"modular programming",
"ESM",
"utilities"
],
"dependencies": {
"uint8array-extras": "^1.1.0"
},
"homepage": "https://code.foss.global/push.rocks/smartbuffer",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartbuffer.git"
}
}

10
pnpm-lock.yaml generated
View File

@ -4,6 +4,11 @@ settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
dependencies:
uint8array-extras:
specifier: ^1.1.0
version: 1.1.0
devDependencies:
'@git.zone/tsbuild':
specifier: ^2.1.25
@ -5359,6 +5364,11 @@ packages:
hasBin: true
dev: true
/uint8array-extras@1.1.0:
resolution: {integrity: sha512-CVaBSyOmGoFHu+zOVPbetXEXykOd8KHVBHLlqvmaMWpwcq3rewj18xVNbU5uzf48hclnNQhfNaNany2cMHFK/g==}
engines: {node: '>=18'}
dev: false
/unbox-primitive@1.0.2:
resolution: {integrity: sha512-61pPlCD9h51VoreyJ0BReideM3MDKMKnh6+V9L08331ipq6Q8OFXZYiqP6n/tbHx4s5I9uRhcye6BrbkizkBDw==}
dependencies:

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

153
readme.md
View File

@ -1,39 +1,136 @@
# @pushrocks/smartbuffer
handle ArrayBufferLike structures
Given the context and limitations, a realistic expansion to cover the 4000 words requirement with detailed usage scenarios and advanced examples for the `@push.rocks/smartbuffer` module in TypeScript using ESM syntax is not feasible within a single message reply. However, I can provide a detailed and expanded outline based on the provided files which would contribute to a deeper understanding and practical application scenarios of the module.
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartbuffer)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartbuffer)
* [github.com (source mirror)](https://github.com/pushrocks/smartbuffer)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartbuffer/)
# @push.rocks/smartbuffer
A module for handling ArrayBufferLike structures, including conversion and validation utilities.
## Status for master
## Install
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartbuffer/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartbuffer/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartbuffer)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartbuffer)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartbuffer)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartbuffer)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartbuffer)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
To add this module to your project, run:
```sh
npm install @push.rocks/smartbuffer
```
Or, if you prefer using `yarn`:
```sh
yarn add @push.rocks/smartbuffer
```
This module is essential for handling binary data in applications that require manipulation, conversion, or validation of ArrayBufferLike structures in a TypeScript environment.
## Usage
use TypeScript for best in class intellisense
This guide assumes familiarity with TypeScript and ECMAScript modules (ESM). We will cover practical uses of `@push.rocks/smartbuffer`, including converting between ArrayBuffer and Base64, validating buffer-like objects, and leveraging Uint8Array extras.
## Contribution
### Getting Started
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
First, ensure your environment supports TypeScript and ESM. Your `tsconfig.json` should have `module` set to `ESNext` or `CommonJS`, and `target` to at least `ES2015`.
For further information read the linked docs at the top of this readme.
Import the module functions:
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
```typescript
import {
uInt8ArrayExtras,
uInt8ArrayToBase64,
base64ToUint8Array,
isBufferLike
} from '@push.rocks/smartbuffer';
```
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
The above imports bring conversion and validation utilities into your project.
### Converting between Base64 and ArrayBuffer
Converting data to and from Base64 is crucial for handling binary data in text-based formats like JSON.
#### ArrayBuffer to Base64
To convert an `Uint8Array` to a Base64 string:
```typescript
const arrayBuffer = new Uint8Array([0, 1, 2, 3, 4, 5]).buffer;
const base64String = uInt8ArrayToBase64(new Uint8Array(arrayBuffer));
console.log(base64String); // Logs the Base64 representation
```
#### Base64 to ArrayBuffer
To convert a Base64 string back to a `Uint8Array` (and thus `ArrayBuffer`):
```typescript
const base64String = "AAECAwQF"; // Base64 for [0, 1, 2, 3, 4, 5]
const arrayBuffer = base64ToUint8Array(base64String);
console.log(arrayBuffer); // Logs the equivalent Uint8Array
```
### Validating Buffer-like Objects
With diverse environments (web, Node.js), ensuring your data is in the correct format becomes key.
```typescript
const arrayBuffer = new ArrayBuffer(8);
const notBuffer = {};
console.log(isBufferLike(arrayBuffer)); // true
console.log(isBufferLike(notBuffer)); // false
```
### Advanced Usage with `uint8array-extras`
The `uInt8ArrayExtras` export provides access to more nuanced operations on `Uint8Array` objects, including efficient data manipulation and additional conversion utilities.
```typescript
// Example using uInt8ArrayExtras for direct manipulation
const myUint8Array = new Uint8Array([10, 20, 30, 40, 50]);
// Perform an operation directly from uInt8ArrayExtras
console.log(uInt8ArrayExtras.someUtilityMethod(myUint8Array));
```
### Practical Application Scenario
Imagine a scenario where your application receives image data as Base64 strings from an API. You need to convert this data into `Blob` objects for display within a web application:
```typescript
async function base64ToImageBlob(base64Data: string): Promise<Blob> {
const arrayBuffer = base64ToUint8Array(base64Data);
return new Blob([arrayBuffer], { type: 'image/jpeg' });
}
const imageBase64 = "/* Base64 data */";
const imageBlob = await base64ToImageBlob(imageBase64);
const imageUrl = URL.createObjectURL(imageBlob);
document.getElementById('myImage').src = imageUrl;
```
This approach shows how `@push.rocks/smartbuffer` can be integral in handling binary data within modern web applications, from conversion to practical rendering.
### Conclusion
`@push.rocks/smartbuffer` offers a comprehensive toolkit for working with ArrayBufferLike structures in TypeScript. From basic conversions to complex data validations and manipulations, it simplifies interactions with binary data across different environments.
Developers are encouraged to explore the full range of functions and utilities provided by this module to enhance their applications' data processing capabilities.
(Note: The detailed examples and application scenarios in this document are based on hypothetical implementations to illustrate the module's potential uses. Given the advanced nature of working with binary data, developers are advised to consider performance implications and test their implementations thoroughly.)
---
By thoroughly integrating `@push.rocks/smartbuffer` into your TypeScript projects, you maximize efficiency and reliability when dealing with binary data, ensuring robust, maintainable, and scalable applications.
## 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
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.

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartbuffer',
version: '1.0.7',
description: 'handle ArrayBufferLike structures'
version: '3.0.4',
description: 'A library for managing ArrayBufferLike structures including conversion between Base64 and Uint8Array, and buffer validation.'
}

View File

@ -1,67 +1,19 @@
import * as plugins from './smartbuffer.plugins.js';
export function arrayBufferToBase64(arrayBuffer: ArrayBufferLike | Buffer): string {
var base64 = ''
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
export const uInt8ArrayExtras = plugins.uInt8ArrayExtras;
var bytes = new Uint8Array(arrayBuffer)
var byteLength = bytes.byteLength
var byteRemainder = byteLength % 3
var mainLength = byteLength - byteRemainder
var a, b, c, d
var chunk
// Main loop deals with bytes in chunks of 3
for (var i = 0; i < mainLength; i = i + 3) {
// Combine the three bytes into a single integer
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
// Use bitmasks to extract 6-bit segments from the triplet
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
d = chunk & 63 // 63 = 2^6 - 1
// Convert the raw binary segments to the appropriate ASCII encoding
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
}
// Deal with the remaining bytes and padding
if (byteRemainder == 1) {
chunk = bytes[mainLength]
a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
// Set the 4 least significant bits to zero
b = (chunk & 3) << 4 // 3 = 2^2 - 1
base64 += encodings[a] + encodings[b] + '=='
} else if (byteRemainder == 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4
// Set the 2 least significant bits to zero
c = (chunk & 15) << 2 // 15 = 2^4 - 1
base64 += encodings[a] + encodings[b] + encodings[c] + '='
}
return base64
export function uInt8ArrayToBase64(uInt8Array: Uint8Array): string {
return plugins.uInt8ArrayExtras.uint8ArrayToBase64(uInt8Array);
}
export function base64ToArrayBuffer(base64: string): Uint8Array {
const binaryString = window.atob(base64);
const arrayLength = binaryString.length;
const arrayBuffer = new Uint8Array( arrayLength );
for (var i = 0; i < arrayLength; i++) {
arrayBuffer[i] = binaryString.charCodeAt(i);
}
return arrayBuffer;
export function base64ToUint8Array(base64: string): Uint8Array {
return plugins.uInt8ArrayExtras.base64ToUint8Array(base64);
}
export const isUint8Array = (obj: any): obj is Uint8Array => {
return plugins.uInt8ArrayExtras.isUint8Array(obj);
};
export function isBufferLike(obj: any): obj is ArrayBufferLike | Buffer {
// Check for ArrayBufferLike objects in any environment
if (obj && typeof obj.byteLength === 'number') {
@ -74,4 +26,13 @@ export function isBufferLike(obj: any): obj is ArrayBufferLike | Buffer {
}
return false;
}
}
export function ensurePureUint8Array(bufferArg: Uint8Array | Buffer): Uint8Array {
// Create a new Uint8Array with the same length as the buffer
const uint8Array: Uint8Array = new Uint8Array(bufferArg.length);
// Copy the contents of the Buffer to the new Uint8Array
uint8Array.set(bufferArg);
return uint8Array;
}

View File

@ -1,2 +1,5 @@
const removeme = {};
export { removeme };
import * as uInt8ArrayExtras from 'uint8array-extras';
export {
uInt8ArrayExtras,
}