deno.land / x / simple_utility@v0.0.9

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 random = await 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.

Date UnixTime

const date = new Date();

const encoded = dateEncode(date); // unixtime in seconds.
const decoded = dateDecode(encoded); // Restored.
const unixtime = dateParse(date.toISOString()); // unixtime in seconds.

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 = ucEncode(text); // byte array in UTF-8 format.
const decoded = ucDecode(encoded); // Restored.
const hexadecimal = hexEncode(encoded); // HEX string.
const formatted = trimExtend(decoded); // formatted string.

Platform Specific

const win = isWin(); // "true" if running on Windows.
const tmp = tmpPath(); // "C:/Windows/Temp" if running on Windows, or "/tmp" if running on Linux or Mac.
cwdMain(); // Move current directory to `Deno.mainModule`.

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.compatible.ts.

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

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

This section may eventually be automated with GitHub Actions, in which case the bundled scripts will be merged into GitHub Releases, making this step unnecessary.

Tips

This section is not directly related to this module, but provides a few line snippets to help you implement your application.

Show more details...

JSON Import with Type

const {default: data} = await import("./data.json", {assert: {type: "json"}});

API

See Deno Document for details.

simple_utility

Version Info

Tagged at
a year ago