mirror of
https://github.com/arfct/itty-bitty.git
synced 2026-03-11 08:54:33 +00:00
This commit introduces the AAH-CLI, a Node.js command-line tool for creating `itty.bitty`-style URL fragments. The CLI includes two main commands: - `encode`: Compresses text content using Gzip or LZMA and outputs a Base64-encoded string. It accepts input from files, direct text strings, or stdin. - `publish`: Encodes content and then publishes the resulting link to a specified file within a GitHub repository. This command uses Git to clone, modify, commit, and push the changes. The project is structured into modular components for encoding (`encoder.js`) and publishing (`publisher.js`), with a manual argument parser in the main `aah-cli.js` file for robustness. Includes comprehensive `README.md` and `AGENTS.md` documentation.
27 lines
874 B
JavaScript
27 lines
874 B
JavaScript
import { deflateSync } from 'zlib';
|
|
import { compress } from 'lzma-native';
|
|
|
|
/**
|
|
* Compresses and encodes text using the specified algorithm.
|
|
*
|
|
* @param {string} text The input text to encode.
|
|
* @param {string} algorithm The compression algorithm to use ('gzip' or 'lzma').
|
|
* @returns {Promise<string>} A promise that resolves with the Base64 encoded string.
|
|
*/
|
|
export async function encode(text, algorithm = 'gzip') {
|
|
const inputBuffer = Buffer.from(text, 'utf8');
|
|
let compressedBuffer;
|
|
|
|
switch (algorithm) {
|
|
case 'lzma':
|
|
compressedBuffer = await compress(inputBuffer, { preset: 9 });
|
|
break;
|
|
case 'gzip':
|
|
compressedBuffer = deflateSync(inputBuffer, { level: 9 });
|
|
break;
|
|
default:
|
|
throw new Error(`Unsupported algorithm: ${algorithm}. Please use 'gzip' or 'lzma'.`);
|
|
}
|
|
|
|
return compressedBuffer.toString('base64');
|
|
}
|