16 Commits

Author SHA1 Message Date
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
066dac4bdc 1.0.7 2024-02-29 22:43:37 +01:00
e1faf34660 fix(core): update 2024-02-29 22:43:37 +01:00
031f6449bd 1.0.6 2024-02-25 01:45:49 +01:00
4f5282e5cd fix(core): update 2024-02-25 01:45:48 +01:00
d42bf699f7 1.0.5 2024-02-25 01:33:16 +01:00
a0d3e8b12f fix(core): update 2024-02-25 01:33:15 +01:00
e343caa204 1.0.4 2024-02-25 01:17:48 +01:00
693a2de3e5 fix(core): update 2024-02-25 01:17:47 +01:00
9ef10edf3d switch to new org scheme 2023-07-11 00:17:11 +02:00
7fee4d8612 switch to new org scheme 2023-07-10 02:42:20 +02:00
12 changed files with 5901 additions and 11282 deletions

View File

@ -2,17 +2,29 @@
"gitzone": { "gitzone": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "code.foss.global",
"gitscope": "pushrocks", "gitscope": "push.rocks",
"gitrepo": "smartbuffer", "gitrepo": "smartbuffer",
"description": "handle ArrayBufferLike structures", "description": "A module for handling ArrayBufferLike structures, including conversion and validation utilities.",
"npmPackagename": "@pushrocks/smartbuffer", "npmPackagename": "@push.rocks/smartbuffer",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks" "projectDomain": "push.rocks",
"keywords": [
"typescript",
"arraybuffer",
"buffer conversion",
"base64",
"buffer validation",
"utility",
"data processing"
]
} }
}, },
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "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"
} }
} }

11165
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
{ {
"name": "@pushrocks/smartbuffer", "name": "@push.rocks/smartbuffer",
"version": "1.0.3", "version": "2.0.0",
"private": false, "private": false,
"description": "handle ArrayBufferLike structures", "description": "A module for handling ArrayBufferLike structures, including conversion and validation utilities.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -10,18 +10,16 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tstest test/ --web)", "test": "(tstest test/ --web)",
"build": "(tsbuild --web --allowimplicitany)", "build": "(tsbuild --web --allowimplicitany)"
"buildDocs": "(tsdoc)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@git.zone/tsbuild": "^2.1.25",
"@gitzone/tsbundle": "^2.0.5", "@git.zone/tsbundle": "^2.0.5",
"@gitzone/tsdoc": "^1.1.10", "@git.zone/tsrun": "^1.2.46",
"@gitzone/tstest": "^1.0.44", "@git.zone/tstest": "^1.0.44",
"@pushrocks/tapbundle": "^5.0.3", "@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^17.0.41" "@types/node": "^20.11.20"
}, },
"dependencies": {},
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"
], ],
@ -36,5 +34,17 @@
"cli.js", "cli.js",
"npmextra.json", "npmextra.json",
"readme.md" "readme.md"
] ],
"keywords": [
"typescript",
"arraybuffer",
"buffer conversion",
"base64",
"buffer validation",
"utility",
"data processing"
],
"dependencies": {
"uint8array-extras": "^1.1.0"
}
} }

5712
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

120
readme.md
View File

@ -1,39 +1,105 @@
# @pushrocks/smartbuffer # @push.rocks/smartbuffer
handle ArrayBufferLike structures handle ArrayBufferLike structures
## Availabililty and Links ## Install
* [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/)
## Status for master To install `@push.rocks/smartbuffer`, use the following command with npm:
Status Category | Status Badge ```sh
-- | -- npm install @push.rocks/smartbuffer --save
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) or with yarn:
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) ```sh
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) yarn add @push.rocks/smartbuffer
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) This will add `@push.rocks/smartbuffer` to your project's dependencies and you'll be ready to use it in your application.
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)
## Usage ## Usage
use TypeScript for best in class intellisense `@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.
## Contribution ### Basic Concepts
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). :) 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.
For further information read the linked docs at the top of this readme. ### Importing the Module
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) First, import the functions you need from the module:
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com) ```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
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.

21
test/test.both.ts Normal file
View File

@ -0,0 +1,21 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as smartbuffer from '../ts/index.js';
tap.test('first test', async () => {
console.log(smartbuffer);
});
tap.test('should recognize different buffer like objects', async () => {
// const testBuffer = Buffer.from('test');
const testArrayBuffer = new ArrayBuffer(4);
const testUint8Array = new Uint8Array(testArrayBuffer);
testUint8Array[0] = 116;
testUint8Array[1] = 101;
testUint8Array[2] = 115;
testUint8Array[3] = 116;
// expect(smartbuffer.isBufferLike(testBuffer)).toBeTrue();
expect(smartbuffer.isBufferLike(testArrayBuffer)).toBeTrue();
expect(smartbuffer.isBufferLike(testUint8Array)).toBeTrue();
});
tap.start();

View File

@ -1,8 +0,0 @@
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
import * as smartbuffer from '../ts/index.js';
tap.test('first test', async () => {
console.log(smartbuffer);
});
tap.start();

View File

@ -2,7 +2,7 @@
* autocreated commitinfo by @pushrocks/commitinfo * autocreated commitinfo by @pushrocks/commitinfo
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartbuffer', name: '@push.rocks/smartbuffer',
version: '1.0.3', version: '2.0.0',
description: 'handle ArrayBufferLike structures' description: 'A module for handling ArrayBufferLike structures, including conversion and validation utilities.'
} }

View File

@ -1,63 +1,25 @@
import * as plugins from './smartbuffer.plugins.js'; import * as plugins from './smartbuffer.plugins.js';
export function arrayBufferToBase64(arrayBuffer: ArrayBufferLike | Buffer): string { export const uInt8ArrayExtras = plugins.uInt8ArrayExtras;
var base64 = ''
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
var bytes = new Uint8Array(arrayBuffer) export function uInt8ArrayToBase64(uInt8Array: Uint8Array): string {
var byteLength = bytes.byteLength return plugins.uInt8ArrayExtras.uint8ArrayToBase64(uInt8Array);
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 base64ToArrayBuffer(base64: string): Uint8Array { export function base64ToUint8Array(base64: string): Uint8Array {
const binaryString = window.atob(base64); return plugins.uInt8ArrayExtras.base64ToUint8Array(base64);
const arrayLength = binaryString.length; }
const arrayBuffer = new Uint8Array( arrayLength );
for (var i = 0; i < arrayLength; i++) { export function isBufferLike(obj: any): obj is ArrayBufferLike | Buffer {
arrayBuffer[i] = binaryString.charCodeAt(i); // Check for ArrayBufferLike objects in any environment
} if (obj && typeof obj.byteLength === 'number') {
return arrayBuffer; return true;
}
// Additional check specific to Node.js environment for Buffer objects
if (typeof Buffer !== 'undefined' && Buffer.isBuffer) {
return Buffer.isBuffer(obj);
}
return false;
} }

View File

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

View File

@ -3,7 +3,12 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"useDefineForClassFields": false, "useDefineForClassFields": false,
"target": "ES2022", "target": "ES2022",
"module": "ES2022", "module": "NodeNext",
"moduleResolution": "nodenext" "moduleResolution": "NodeNext",
} "esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
} }