itty-bitty/encoder.js
google-labs-jules[bot] be79518f7f feat: Implement Agent-Assisted Hypermedia Encoding CLI (AAH-CLI)
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.
2025-09-08 11:08:09 +00:00

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');
}