Skip to main content
Deno 2 is finally here šŸŽ‰ļø
Learn more

Simple Utility for Deno

Actions-Test Actions-Release

A handy utility collection.

Example

Show more detailsā€¦

BASE64 Binary

const file = await Deno.readFile("/path/to/binary.bin");

const encoded = base64Encode(file); // base64 code.
const decoded = base64Decode(encoded); // Restored.

Easy WebCrypto

const file = await Deno.readFile("/path/to/binary.bin");

const uuid = cryptoUuid(); // random UUID.
const random = cryptoRandom(16); // random byte array.
const hash = await cryptoHash(true, file); // byte array of SHA2 512 bits hash value.
const keyEcdh = await cryptoGenerateKey(true); // public/private key pair for ECDH, each in byte array.
const keyEcdsa = await cryptoGenerateKey(false); // public/private key pair for ECDSA, each in byte array.
const encrypted = await cryptoEncrypt(keyEcdh, file); // encrypted byte array.
const decrypted = await cryptoDecrypt(keyEcdh, encrypted); // Restored.
const signature = await cryptoSign(keyEcdsa.privateKey, data); // signature byte array.
const verify = await cryptoVerify(signature, keyEcdsa.publicKey, data); // `true` if correct.

DEFLATE Compress

const file = await Deno.readFile("/path/to/binary.bin");

const encoded = await deflateEncode(file); // "deflate" compressed byte array.
const decoded = await deflateDecode(encoded); // Restored.

Extended Fetch API

const json = await fetchExtend("https://path/to/get", "json"); // response as JSON.
const bytes = await fetchExtend("https://path/to/get", "byte"); // response as Uint8Array.

Minipack Archive

const files = [
    ["binary.bin", Deno.readFileSync("/path/to/binary.bin")]
];

const encoded = await minipackEncode(files); // byte array in "minipack" format.
const decoded = await minipackDecode(encoded); // Restored.

Text Convert

const text = " Lorem ipsum  \t  dolor \r sit amet.";

const encoded = utfEncode(text); // byte array in UTF-8 format.
const decoded = utfDecode(encoded); // Restored.
const hexadecimal = hexEncode(encoded); // HEX string.
const formatted = trimExtend(decoded); // formatted string.

UnixTime Date

const date = new Date();

const encoded = unixtimeEncode(date); // unixtime in seconds.
const decoded = unixtimeDecode(encoded); // Restored.
const unixtime = unixtimeParse(date.toISOString()); // unixtime in seconds.

Path Operation (Deno Only)

const posix = posixSep("C:\\Users\\Administrator"); // POSIX style (slash) path string.
const win = winSep("C:/Users/Administrator"); // Windows style (backslash) path string.
const tmp = tmpPath(); // `/tmp` if running on Linux or Mac, `C:/Windows/Temp` if running on Windows.
const data = dataPath(); // `/var` if running on Linux or Mac, `C:/ProgramData` if running on Windows.
const home = homePath(); // `$HOME` if running on Linux or Mac, `%USERPROFILE%` if running on Windows.
const main = mainPath(); // Returns the directory of `Deno.mainModule`.

Platform Specific (Deno Only)

const iswin = isWin(); // "true" if running on Windows.

Details

Itā€™s basically a thin wrapper around Denoā€™s functions to improve usability, but some features are original to this module.

This section describes the original features of this module.

Minipack

ā€œMinipackā€ is a file archive format original to this module.

Itā€™s structure is inspired by the famous ā€œtarā€ and is minimal as an archive.

Originally developed for web browser, the purpose was to aggregate multiple files input with the HTML File API into a single binary.

Therefore, there is no concept of directory or filesystem, and itā€™s feature by simple structure that stores only the file body, file name, and hash value for verification.

The actual binary structure looks like this:

Index Type Title Size (Byte)
1 Header HashValue 32
2 Header NameSize 1
3 Header BodySize 4
4 Body FileName Max 255 (Defined in NameSize)
5 Body FileBody Max 4294967295 (Defined in BodySize)

This is for one file and repeats for the number of files.

Browser Compatible

Some methods and classes in this module donā€™t use globalThis.Deno internally and are browser compatible.

I have prepared browser compatible code only export as mod.universal.ts.

By bundling this, you can easily create universal utility scripts.

deno bundle https://deno.land/x/simple_utility@(version)/mod.universal.ts > ./simple_utility.js

API

See Deno Document for details.