Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb89564339 | |||
f480be9917 | |||
11bf0d9dbb | |||
c58cc2b0df | |||
87273cbbbd | |||
bebc8b0ede | |||
46e5217fc0 | |||
ebb539e824 | |||
63e3b492e6 | |||
158614c62c | |||
f11114f6dc | |||
f233cb1dcd | |||
866da6f21a | |||
30ac0ddbb4 | |||
4c5105aae7 | |||
1f6e257cb7 | |||
0a79087c59 | |||
432a59bcec | |||
10d7c5d8e8 | |||
30306f3a40 | |||
9a4859f6a0 | |||
099a68d19f | |||
78576b363e |
@ -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"
|
||||
}
|
||||
}
|
28
package.json
28
package.json
@ -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
10
pnpm-lock.yaml
generated
@ -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
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
153
readme.md
153
readme.md
@ -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 | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](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';
|
||||
```
|
||||
|
||||
[](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.
|
||||
|
@ -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.'
|
||||
}
|
||||
|
77
ts/index.ts
77
ts/index.ts
@ -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;
|
||||
}
|
||||
|
@ -1,2 +1,5 @@
|
||||
const removeme = {};
|
||||
export { removeme };
|
||||
import * as uInt8ArrayExtras from 'uint8array-extras';
|
||||
|
||||
export {
|
||||
uInt8ArrayExtras,
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user