UNPKG

@hugoalh/djb2a

Version:

A CLI and module to get the non-cryptographic hash of the data with algorithm DJB2a.

114 lines (113 loc) 4.5 kB
#!/usr/bin/env node import "./_dnt.polyfills.js"; import * as dntShim from "./_dnt.shims.js"; var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { if (value !== null && value !== void 0) { if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); var dispose, inner; if (async) { if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); dispose = value[Symbol.asyncDispose]; } if (dispose === void 0) { if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); dispose = value[Symbol.dispose]; if (async) inner = dispose; } if (typeof dispose !== "function") throw new TypeError("Object not disposable."); if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; env.stack.push({ value: value, dispose: dispose, async: async }); } else if (async) { env.stack.push({ async: true }); } return value; }; var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { return function (env) { function fail(e) { env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; env.hasError = true; } var r, s = 0; function next() { while (r = env.stack.pop()) { try { if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); if (r.dispose) { var result = r.dispose.call(r.value); if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); } else s |= 1; } catch (e) { fail(e); } } if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); if (env.hasError) throw env.error; } return next(); }; })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }); import { parseArgs } from "./deps/jsr.io/@std/cli/1.0.10/parse_args.js"; import { DJB2a } from "./mod.js"; if (!(import.meta.url === ("file:///" + process.argv[1].replace(/\\/g, "/")).replace(/\/{3,}/, "///"))) { throw new Error(`This script is for command line usage only!`); } const args = parseArgs(dntShim.Deno.args, { boolean: [ "file", "stdin" ] }); const fromFile = args.file; const fromStdin = args.stdin; const argsValues = args._.map((value) => { return String(value); }); if (fromFile && fromStdin) { throw new SyntaxError(`Unable to use the sources of file and stdin together!`); } if (fromFile) { const env_1 = { stack: [], error: void 0, hasError: false }; try { if (argsValues.length === 0) { throw new SyntaxError(`File path is not defined!`); } if (argsValues.length !== 1) { throw new SyntaxError(`Too many arguments! Expect: 1; Current: ${argsValues.length}.`); } const file = __addDisposableResource(env_1, await dntShim.Deno.open(argsValues[0]), false); console.log((await DJB2a.fromStream(file.readable)).hashHexPadding()); } catch (e_1) { env_1.error = e_1; env_1.hasError = true; } finally { __disposeResources(env_1); } } else if (fromStdin) { if (argsValues.length !== 0) { throw new SyntaxError(`Too many arguments! Expect: 0; Current: ${argsValues.length}.`); } let data = Uint8Array.from([]); for await (const chunk of dntShim.Deno.stdin.readable) { data = Uint8Array.from([...data, ...chunk]); } console.log(new DJB2a(new TextDecoder().decode(data).replace(/\r?\n$/, "")).hashHexPadding()); } else { if (argsValues.length === 0) { throw new SyntaxError(`Data is not defined!`); } if (argsValues.length !== 1) { throw new SyntaxError(`Too many arguments! Expect: 1; Current: ${argsValues.length}.`); } console.log(new DJB2a(argsValues[0]).hashHexPadding()); }