aws-local-stepfunctions
Version:
Execute an AWS Step Function state machine locally
1,634 lines (1,593 loc) • 106 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
ExecutionAbortedError: () => ExecutionAbortedError,
ExecutionError: () => ExecutionError,
ExecutionTimeoutError: () => ExecutionTimeoutError,
StateMachine: () => StateMachine
});
module.exports = __toCommonJS(main_exports);
// src/error/ExecutionAbortedError.ts
var ExecutionAbortedError = class extends Error {
constructor() {
super("Execution aborted");
this.name = "ExecutionAbortedError";
}
};
// src/error/ExecutionTimeoutError.ts
var ExecutionTimeoutError = class extends Error {
constructor() {
super("Execution timed out");
this.name = "ExecutionTimeoutError";
}
};
// src/error/ExecutionError.ts
var ExecutionError = class extends Error {
constructor(caughtError) {
super(`Execution has failed with the following error: ${caughtError.message}`, { cause: caughtError });
this.name = "ExecutionError";
}
};
// src/util/random.ts
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ h1 >>> 18, 597399067);
h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233);
h3 = Math.imul(h1 ^ h3 >>> 17, 951274213);
h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179);
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
}
function sfc32(a, b, c, d) {
return function() {
a >>>= 0;
b >>>= 0;
c >>>= 0;
d >>>= 0;
let t = a + b | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = c << 21 | c >>> 11;
d = d + 1 | 0;
t = t + d | 0;
c = c + t | 0;
return (t >>> 0) / 4294967296;
};
}
function getRandomNumber(min, max, rng = Math.random) {
return Math.floor(rng() * (max - min + 1)) + min;
}
// src/util/index.ts
var isBrowserEnvironment = typeof window !== "undefined" && typeof window.document !== "undefined";
function isPlainObj(value) {
return !!value && Object.getPrototypeOf(value) === Object.prototype;
}
function sleep(ms, abortSignal) {
if (ms === 0) return;
return new Promise((resolve) => {
if (abortSignal?.aborted) {
return resolve();
}
const onAbort = () => {
clearTimeout(timeout);
resolve();
};
const timeout = setTimeout(() => {
abortSignal?.removeEventListener("abort", onAbort);
resolve();
}, ms);
abortSignal?.addEventListener("abort", onAbort, { once: true });
});
}
function byteToHex(byte) {
return byte.toString(16).padStart(2, "0");
}
function isRFC3339Timestamp(date) {
const regex = /^\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.\d+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9]))$/;
return regex.test(date);
}
function stringifyJSONValue(value) {
return JSON.stringify(value);
}
function clamp(value, min, max) {
if (min && value < min) return min;
if (max && value > max) return max;
return value;
}
// src/stateMachine/jsonPath/JsonPath.ts
var import_jsonpath_plus = require("jsonpath-plus");
// src/stateMachine/jsonPath/constraints/BaseJsonPathConstraint.ts
var BaseJSONPathConstraint = class {
pathExpression;
constructor(pathExpression) {
this.pathExpression = pathExpression;
}
};
// src/error/RuntimeError.ts
var RuntimeError = class extends Error {
/**
* Whether this runtime error can be matched in a `Retry` field
*/
retryable;
/**
* Whether this runtime error can be caught in a `Catch` field
*/
catchable;
constructor(message, cause) {
super(message, { cause });
this.name = "RuntimeError";
this.retryable = true;
this.catchable = true;
}
get isRetryable() {
return this.retryable;
}
get isCatchable() {
return this.catchable;
}
};
// src/error/predefined/StatesRuntimeError.ts
var StatesRuntimeError = class extends RuntimeError {
constructor(message = "States.Runtime") {
super(message);
this.name = "States.Runtime";
this.retryable = false;
this.catchable = false;
}
};
// src/stateMachine/jsonPath/constraints/DefinedValueConstraint.ts
var DefinedValueConstraint = class extends BaseJSONPathConstraint {
test(value) {
if (typeof value === "undefined") {
throw new StatesRuntimeError(`Path expression '${this.pathExpression}' does not point to a value`);
}
}
};
// src/stateMachine/jsonPath/JsonPath.ts
function jsonPathQuery(pathExpression, json, context, options) {
const defaultConstraints = [];
if (!options?.ignoreDefinedValueConstraint) {
defaultConstraints.push(DefinedValueConstraint);
}
const constraints = [...defaultConstraints, ...options?.constraints ?? []];
let evaluation;
if (pathExpression.startsWith("$$")) {
evaluation = (0, import_jsonpath_plus.JSONPath)({ path: pathExpression.slice(1), json: context ?? null, wrap: false });
} else {
evaluation = (0, import_jsonpath_plus.JSONPath)({ path: pathExpression, json, wrap: false });
}
for (const Constraint of constraints) {
const constraintObject = new Constraint(pathExpression);
constraintObject.test(evaluation);
}
return evaluation;
}
// src/error/predefined/StatesResultPathMatchFailureError.ts
var StatesResultPathMatchFailureError = class extends RuntimeError {
constructor() {
super("States.ResultPathMatchFailure");
this.name = "States.ResultPathMatchFailure";
}
};
// src/stateMachine/intrinsicFunctions/ArgumentHandling.ts
function validateArgumentType(allowedTypes, argPosition, funcArg, funcName) {
let matchesAllowedType = false;
for (const argType of allowedTypes) {
switch (argType) {
case "string":
case "number":
case "boolean":
matchesAllowedType = typeof funcArg === argType;
break;
case "null":
matchesAllowedType = funcArg === null;
break;
case "array":
matchesAllowedType = Array.isArray(funcArg);
break;
case "object":
matchesAllowedType = isPlainObj(funcArg);
break;
case "any":
matchesAllowedType = true;
break;
}
if (matchesAllowedType) break;
}
const expectedType = allowedTypes.map((type) => `'${type}'`).join(" | ");
if (!matchesAllowedType) {
throw new StatesRuntimeError(
`Intrinsic function ${funcName} expected argument ${argPosition} to be of type ${expectedType}, but received ${typeof funcArg}`
);
}
}
function validateArgumentConstraints(argConstraints, argPosition, funcArg, funcName) {
if (argConstraints) {
let matchesAllConstraints = false;
for (const constraint of argConstraints) {
switch (constraint) {
case "ZERO":
matchesAllConstraints = funcArg === 0;
break;
case "POSITIVE_INTEGER":
matchesAllConstraints = Number.isInteger(funcArg) && funcArg > 0;
break;
case "NEGATIVE_INTEGER":
matchesAllConstraints = Number.isInteger(funcArg) && funcArg < 0;
break;
case "INTEGER":
matchesAllConstraints = Number.isInteger(funcArg);
break;
}
if (matchesAllConstraints) break;
}
const expectedConstraints = argConstraints.map((constraint) => `'${constraint}'`).join(" | ");
if (!matchesAllConstraints) {
throw new StatesRuntimeError(
`Intrinsic function ${funcName} expected argument ${argPosition} to satisfy the following constraints: ${expectedConstraints}`
);
}
}
}
function validateArguments(funcDefinition, ...args) {
if ("exactArgs" in funcDefinition && args.length !== funcDefinition.exactArgs) {
throw new StatesRuntimeError(
`Intrinsic function ${funcDefinition.name} expects exactly ${funcDefinition.exactArgs} arguments, but received ${args.length}`
);
}
if ("minArgs" in funcDefinition && args.length < funcDefinition.minArgs) {
throw new StatesRuntimeError(
`Intrinsic function ${funcDefinition.name} expects at least ${funcDefinition.minArgs} arguments, but received ${args.length}`
);
}
if ("maxArgs" in funcDefinition && args.length > funcDefinition.maxArgs) {
throw new StatesRuntimeError(
`Intrinsic function ${funcDefinition.name} expects at most ${funcDefinition.maxArgs} arguments, but received ${args.length}`
);
}
if ("arguments" in funcDefinition) {
for (let i = 0; i < funcDefinition.arguments.length; i++) {
const argDefinition = funcDefinition.arguments[i];
const funcArg = args[i];
if (funcArg === void 0) break;
validateArgumentType(argDefinition.allowedTypes, i + 1, funcArg, funcDefinition.name);
validateArgumentConstraints(argDefinition.constraints, i + 1, funcArg, funcDefinition.name);
}
}
if ("variadicArguments" in funcDefinition) {
const argDefinition = funcDefinition.variadicArguments;
for (let i = funcDefinition.arguments?.length ?? 0; i < args.length; i++) {
const funcArg = args[i];
validateArgumentType(argDefinition.allowedTypes, i + 1, funcArg, funcDefinition.name);
validateArgumentConstraints(argDefinition.constraints, i + 1, funcArg, funcDefinition.name);
}
}
}
function parseArguments(input, context, ...args) {
return args.map((arg) => {
if (arg[0] === "'") {
const lastSingleQuote = arg.lastIndexOf("'");
return arg.slice(1, lastSingleQuote);
}
if (arg[0] === "$") {
return jsonPathQuery(arg, input, context);
}
return JSON.parse(arg);
});
}
// src/stateMachine/intrinsicFunctions/BaseIntrinsicFunction.ts
var BaseIntrinsicFunction = class {
call(input, context, ...args) {
const parsedArgs = parseArguments(input, context, ...args);
validateArguments(this.funcDefinition, ...parsedArgs);
let result = this.execute(...parsedArgs);
if (typeof result === "string") {
result = result.replaceAll("\\", "");
}
return result;
}
};
// src/stateMachine/intrinsicFunctions/StatesFormat.ts
var StatesFormat = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.Format",
minArgs: 1,
arguments: [
{
allowedTypes: ["string"]
}
],
variadicArguments: {
allowedTypes: ["string", "boolean", "number", "null"]
}
};
}
execute(templateStr, ...placeholderValues) {
const placeholdersNumber = templateStr.match(/\{\}/g)?.length ?? 0;
if (placeholdersNumber !== placeholderValues.length) {
throw new StatesRuntimeError(
`Number of arguments in ${this.funcDefinition.name} do not match the occurrences of {}`
);
}
let i = 0;
return templateStr.replace(/\{\}/g, () => placeholderValues[i++]);
}
};
// src/stateMachine/intrinsicFunctions/StatesStringToJson.ts
var StatesStringToJson = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.StringToJson",
exactArgs: 1,
arguments: [
{
allowedTypes: ["string"]
}
]
};
}
execute(jsonString) {
return JSON.parse(jsonString);
}
};
// src/stateMachine/intrinsicFunctions/StatesJsonToString.ts
var StatesJsonToString = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.JsonToString",
exactArgs: 1,
arguments: [
{
allowedTypes: ["any"]
}
]
};
}
execute(json) {
return JSON.stringify(json);
}
};
// src/stateMachine/intrinsicFunctions/StatesArray.ts
var StatesArray = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.Array",
variadicArguments: {
allowedTypes: ["any"]
}
};
}
execute(...args) {
return args;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayPartition.ts
var StatesArrayPartition = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayPartition",
exactArgs: 2,
arguments: [
{
allowedTypes: ["array"]
},
{
allowedTypes: ["number"],
constraints: ["POSITIVE_INTEGER"]
}
]
};
}
execute(array, chunkSize) {
const partitionedArr = [];
for (let i = 0; i < array.length; i += chunkSize) {
const subArray = array.slice(i, i + chunkSize);
partitionedArr.push(subArray);
}
return partitionedArr;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayContains.ts
var import_isEqual = __toESM(require("lodash/isEqual.js"), 1);
var StatesArrayContains = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayContains",
exactArgs: 2,
arguments: [
{
allowedTypes: ["array"]
},
{
allowedTypes: ["any"]
}
]
};
}
execute(array, searchVal) {
return array.findIndex((val) => (0, import_isEqual.default)(val, searchVal)) > -1;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayRange.ts
var StatesArrayRange = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayRange",
exactArgs: 3,
arguments: [
{
allowedTypes: ["number"],
constraints: ["INTEGER"]
},
{
allowedTypes: ["number"],
constraints: ["INTEGER"]
},
{
allowedTypes: ["number"],
constraints: ["POSITIVE_INTEGER", "NEGATIVE_INTEGER"]
}
]
};
}
execute(start, end, step) {
const slots = (end - start) / step;
if (slots < 0) {
return [];
}
const arrLength = Math.floor(slots) + 1;
if (arrLength > 1e3) {
throw new StatesRuntimeError(
`Result of intrinsic function ${this.funcDefinition.name} cannot contain more than 1000 items`
);
}
const range = Array.from({ length: arrLength }).map((_, i) => start + step * i);
return range;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayGetItem.ts
var StatesArrayGetItem = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayGetItem",
exactArgs: 2,
arguments: [
{
allowedTypes: ["array"]
},
{
allowedTypes: ["number"]
}
]
};
}
execute(array, index) {
return array[index] ?? null;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayLength.ts
var StatesArrayLength = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayLength",
exactArgs: 1,
arguments: [
{
allowedTypes: ["array"]
}
]
};
}
execute(array) {
return array.length;
}
};
// src/stateMachine/intrinsicFunctions/StatesArrayUnique.ts
var import_isEqual2 = __toESM(require("lodash/isEqual.js"), 1);
var import_uniqWith = __toESM(require("lodash/uniqWith.js"), 1);
var StatesArrayUnique = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.ArrayUnique",
exactArgs: 1,
arguments: [
{
allowedTypes: ["array"]
}
]
};
}
execute(array) {
return (0, import_uniqWith.default)(array, import_isEqual2.default);
}
};
// src/stateMachine/intrinsicFunctions/StatesBase64Encode.ts
var StatesBase64Encode = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.Base64Encode",
exactArgs: 1,
arguments: [
{
allowedTypes: ["string"]
}
]
};
}
execute(str) {
if (str.length > 1e4) {
throw new StatesRuntimeError(
`Intrinsic function ${this.funcDefinition.name} cannot encode a string with more than 10,000 characters`
);
}
return btoa(str);
}
};
// src/stateMachine/intrinsicFunctions/StatesBase64Decode.ts
var StatesBase64Decode = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.Base64Decode",
exactArgs: 1,
arguments: [
{
allowedTypes: ["string"]
}
]
};
}
execute(str) {
if (str.length > 1e4) {
throw new StatesRuntimeError(
`Intrinsic function ${this.funcDefinition.name} cannot decode a Base64 string with more than 10,000 characters`
);
}
return atob(str);
}
};
// src/util/hash/BaseHash.ts
var BaseHashAlgorithm = class {
textEncoder;
constructor() {
this.textEncoder = new TextEncoder();
}
getDigest(input) {
const message = this.textEncoder.encode(input);
const paddedMessage = this.padMessage(message);
const hash = this.computeHash(paddedMessage);
const digest = this.hashToString(hash);
return digest;
}
rotl(n, x) {
if (typeof n === "number" && typeof x === "number") return x << n | x >>> 32 - n;
if (typeof n === "bigint" && typeof x === "bigint") return x << n | x >> 64n - n;
throw new Error("Both arguments must be of the same type");
}
rotr(n, x) {
if (typeof n === "number" && typeof x === "number") return x >>> n | x << 32 - n;
if (typeof n === "bigint" && typeof x === "bigint") return x >> n | x << 64n - n;
throw new Error("Both arguments must be of the same type");
}
ch(x, y, z) {
if (typeof x === typeof y && typeof y === typeof z) {
return x & y ^ ~x & z;
}
throw new Error("All arguments must be of the same type");
}
parity(x, y, z) {
if (typeof x === typeof y && typeof y === typeof z) {
return x ^ y ^ z;
}
throw new Error("All arguments must be of the same type");
}
maj(x, y, z) {
if (typeof x === typeof y && typeof y === typeof z) {
return x & y ^ x & z ^ y & z;
}
throw new Error("All arguments must be of the same type");
}
bigSigma0(x) {
if (typeof x === "number") {
return this.rotr(2, x) ^ this.rotr(13, x) ^ this.rotr(22, x);
}
return this.rotr(28n, x) ^ this.rotr(34n, x) ^ this.rotr(39n, x);
}
bigSigma1(x) {
if (typeof x === "number") {
return this.rotr(6, x) ^ this.rotr(11, x) ^ this.rotr(25, x);
}
return this.rotr(14n, x) ^ this.rotr(18n, x) ^ this.rotr(41n, x);
}
smallSigma0(x) {
if (typeof x === "number") {
return this.rotr(7, x) ^ this.rotr(18, x) ^ x >>> 3;
}
return this.rotr(1n, x) ^ this.rotr(8n, x) ^ x >> 7n;
}
smallSigma1(x) {
if (typeof x === "number") {
return this.rotr(17, x) ^ this.rotr(19, x) ^ x >>> 10;
}
return this.rotr(19n, x) ^ this.rotr(61n, x) ^ x >> 6n;
}
};
// src/util/hash/MD5.ts
var MD5 = class extends BaseHashAlgorithm {
padMessage(message) {
const msgLenMod64 = message.length % 64;
let bytesToAdd = 64 - msgLenMod64;
if (msgLenMod64 >= 56) {
bytesToAdd += 64;
}
const padding = new Uint8Array(bytesToAdd);
const paddedMsg = new Uint8Array([...message, ...padding]);
const dataView = new DataView(paddedMsg.buffer);
dataView.setUint8(message.length, 128);
dataView.setBigUint64(paddedMsg.length - 8, BigInt(message.length * 8), true);
return paddedMsg.buffer;
}
computeHash(paddedMessage) {
const b1 = new Uint32Array(4);
const b2 = new Uint32Array([1732584193, 4023233417, 2562383102, 271733878]);
const seq = new Uint32Array(16);
const table = new Uint32Array([
3614090360,
3905402710,
606105819,
3250441966,
4118548399,
1200080426,
2821735955,
4249261313,
1770035416,
2336552879,
4294925233,
2304563134,
1804603682,
4254626195,
2792965006,
1236535329,
4129170786,
3225465664,
643717713,
3921069994,
3593408605,
38016083,
3634488961,
3889429448,
568446438,
3275163606,
4107603335,
1163531501,
2850285829,
4243563512,
1735328473,
2368359562,
4294588738,
2272392833,
1839030562,
4259657740,
2763975236,
1272893353,
4139469664,
3200236656,
681279174,
3936430074,
3572445317,
76029189,
3654602809,
3873151461,
530742520,
3299628645,
4096336452,
1126891415,
2878612391,
4237533241,
1700485571,
2399980690,
4293915773,
2240044497,
1873313359,
4264355552,
2734768916,
1309151649,
4149444226,
3174756917,
718787259,
3951481745
]);
const dataView = new DataView(paddedMessage);
for (let i = 0; i < paddedMessage.byteLength / 4; i += 16) {
for (let t = 0; t < 16; t++) {
seq[t] = dataView.getUint32(i + t * 4, true);
}
b1[0] = b2[0];
b1[1] = b2[1];
b1[2] = b2[2];
b1[3] = b2[3];
b1[0] = b1[1] + this.rotl(7, b1[0] + this.F(b1[1], b1[2], b1[3]) + seq[0] + table[0]);
b1[3] = b1[0] + this.rotl(12, b1[3] + this.F(b1[0], b1[1], b1[2]) + seq[1] + table[1]);
b1[2] = b1[3] + this.rotl(17, b1[2] + this.F(b1[3], b1[0], b1[1]) + seq[2] + table[2]);
b1[1] = b1[2] + this.rotl(22, b1[1] + this.F(b1[2], b1[3], b1[0]) + seq[3] + table[3]);
b1[0] = b1[1] + this.rotl(7, b1[0] + this.F(b1[1], b1[2], b1[3]) + seq[4] + table[4]);
b1[3] = b1[0] + this.rotl(12, b1[3] + this.F(b1[0], b1[1], b1[2]) + seq[5] + table[5]);
b1[2] = b1[3] + this.rotl(17, b1[2] + this.F(b1[3], b1[0], b1[1]) + seq[6] + table[6]);
b1[1] = b1[2] + this.rotl(22, b1[1] + this.F(b1[2], b1[3], b1[0]) + seq[7] + table[7]);
b1[0] = b1[1] + this.rotl(7, b1[0] + this.F(b1[1], b1[2], b1[3]) + seq[8] + table[8]);
b1[3] = b1[0] + this.rotl(12, b1[3] + this.F(b1[0], b1[1], b1[2]) + seq[9] + table[9]);
b1[2] = b1[3] + this.rotl(17, b1[2] + this.F(b1[3], b1[0], b1[1]) + seq[10] + table[10]);
b1[1] = b1[2] + this.rotl(22, b1[1] + this.F(b1[2], b1[3], b1[0]) + seq[11] + table[11]);
b1[0] = b1[1] + this.rotl(7, b1[0] + this.F(b1[1], b1[2], b1[3]) + seq[12] + table[12]);
b1[3] = b1[0] + this.rotl(12, b1[3] + this.F(b1[0], b1[1], b1[2]) + seq[13] + table[13]);
b1[2] = b1[3] + this.rotl(17, b1[2] + this.F(b1[3], b1[0], b1[1]) + seq[14] + table[14]);
b1[1] = b1[2] + this.rotl(22, b1[1] + this.F(b1[2], b1[3], b1[0]) + seq[15] + table[15]);
b1[0] = b1[1] + this.rotl(5, b1[0] + this.G(b1[1], b1[2], b1[3]) + seq[1] + table[16]);
b1[3] = b1[0] + this.rotl(9, b1[3] + this.G(b1[0], b1[1], b1[2]) + seq[6] + table[17]);
b1[2] = b1[3] + this.rotl(14, b1[2] + this.G(b1[3], b1[0], b1[1]) + seq[11] + table[18]);
b1[1] = b1[2] + this.rotl(20, b1[1] + this.G(b1[2], b1[3], b1[0]) + seq[0] + table[19]);
b1[0] = b1[1] + this.rotl(5, b1[0] + this.G(b1[1], b1[2], b1[3]) + seq[5] + table[20]);
b1[3] = b1[0] + this.rotl(9, b1[3] + this.G(b1[0], b1[1], b1[2]) + seq[10] + table[21]);
b1[2] = b1[3] + this.rotl(14, b1[2] + this.G(b1[3], b1[0], b1[1]) + seq[15] + table[22]);
b1[1] = b1[2] + this.rotl(20, b1[1] + this.G(b1[2], b1[3], b1[0]) + seq[4] + table[23]);
b1[0] = b1[1] + this.rotl(5, b1[0] + this.G(b1[1], b1[2], b1[3]) + seq[9] + table[24]);
b1[3] = b1[0] + this.rotl(9, b1[3] + this.G(b1[0], b1[1], b1[2]) + seq[14] + table[25]);
b1[2] = b1[3] + this.rotl(14, b1[2] + this.G(b1[3], b1[0], b1[1]) + seq[3] + table[26]);
b1[1] = b1[2] + this.rotl(20, b1[1] + this.G(b1[2], b1[3], b1[0]) + seq[8] + table[27]);
b1[0] = b1[1] + this.rotl(5, b1[0] + this.G(b1[1], b1[2], b1[3]) + seq[13] + table[28]);
b1[3] = b1[0] + this.rotl(9, b1[3] + this.G(b1[0], b1[1], b1[2]) + seq[2] + table[29]);
b1[2] = b1[3] + this.rotl(14, b1[2] + this.G(b1[3], b1[0], b1[1]) + seq[7] + table[30]);
b1[1] = b1[2] + this.rotl(20, b1[1] + this.G(b1[2], b1[3], b1[0]) + seq[12] + table[31]);
b1[0] = b1[1] + this.rotl(4, b1[0] + this.H(b1[1], b1[2], b1[3]) + seq[5] + table[32]);
b1[3] = b1[0] + this.rotl(11, b1[3] + this.H(b1[0], b1[1], b1[2]) + seq[8] + table[33]);
b1[2] = b1[3] + this.rotl(16, b1[2] + this.H(b1[3], b1[0], b1[1]) + seq[11] + table[34]);
b1[1] = b1[2] + this.rotl(23, b1[1] + this.H(b1[2], b1[3], b1[0]) + seq[14] + table[35]);
b1[0] = b1[1] + this.rotl(4, b1[0] + this.H(b1[1], b1[2], b1[3]) + seq[1] + table[36]);
b1[3] = b1[0] + this.rotl(11, b1[3] + this.H(b1[0], b1[1], b1[2]) + seq[4] + table[37]);
b1[2] = b1[3] + this.rotl(16, b1[2] + this.H(b1[3], b1[0], b1[1]) + seq[7] + table[38]);
b1[1] = b1[2] + this.rotl(23, b1[1] + this.H(b1[2], b1[3], b1[0]) + seq[10] + table[39]);
b1[0] = b1[1] + this.rotl(4, b1[0] + this.H(b1[1], b1[2], b1[3]) + seq[13] + table[40]);
b1[3] = b1[0] + this.rotl(11, b1[3] + this.H(b1[0], b1[1], b1[2]) + seq[0] + table[41]);
b1[2] = b1[3] + this.rotl(16, b1[2] + this.H(b1[3], b1[0], b1[1]) + seq[3] + table[42]);
b1[1] = b1[2] + this.rotl(23, b1[1] + this.H(b1[2], b1[3], b1[0]) + seq[6] + table[43]);
b1[0] = b1[1] + this.rotl(4, b1[0] + this.H(b1[1], b1[2], b1[3]) + seq[9] + table[44]);
b1[3] = b1[0] + this.rotl(11, b1[3] + this.H(b1[0], b1[1], b1[2]) + seq[12] + table[45]);
b1[2] = b1[3] + this.rotl(16, b1[2] + this.H(b1[3], b1[0], b1[1]) + seq[15] + table[46]);
b1[1] = b1[2] + this.rotl(23, b1[1] + this.H(b1[2], b1[3], b1[0]) + seq[2] + table[47]);
b1[0] = b1[1] + this.rotl(6, b1[0] + this.I(b1[1], b1[2], b1[3]) + seq[0] + table[48]);
b1[3] = b1[0] + this.rotl(10, b1[3] + this.I(b1[0], b1[1], b1[2]) + seq[7] + table[49]);
b1[2] = b1[3] + this.rotl(15, b1[2] + this.I(b1[3], b1[0], b1[1]) + seq[14] + table[50]);
b1[1] = b1[2] + this.rotl(21, b1[1] + this.I(b1[2], b1[3], b1[0]) + seq[5] + table[51]);
b1[0] = b1[1] + this.rotl(6, b1[0] + this.I(b1[1], b1[2], b1[3]) + seq[12] + table[52]);
b1[3] = b1[0] + this.rotl(10, b1[3] + this.I(b1[0], b1[1], b1[2]) + seq[3] + table[53]);
b1[2] = b1[3] + this.rotl(15, b1[2] + this.I(b1[3], b1[0], b1[1]) + seq[10] + table[54]);
b1[1] = b1[2] + this.rotl(21, b1[1] + this.I(b1[2], b1[3], b1[0]) + seq[1] + table[55]);
b1[0] = b1[1] + this.rotl(6, b1[0] + this.I(b1[1], b1[2], b1[3]) + seq[8] + table[56]);
b1[3] = b1[0] + this.rotl(10, b1[3] + this.I(b1[0], b1[1], b1[2]) + seq[15] + table[57]);
b1[2] = b1[3] + this.rotl(15, b1[2] + this.I(b1[3], b1[0], b1[1]) + seq[6] + table[58]);
b1[1] = b1[2] + this.rotl(21, b1[1] + this.I(b1[2], b1[3], b1[0]) + seq[13] + table[59]);
b1[0] = b1[1] + this.rotl(6, b1[0] + this.I(b1[1], b1[2], b1[3]) + seq[4] + table[60]);
b1[3] = b1[0] + this.rotl(10, b1[3] + this.I(b1[0], b1[1], b1[2]) + seq[11] + table[61]);
b1[2] = b1[3] + this.rotl(15, b1[2] + this.I(b1[3], b1[0], b1[1]) + seq[2] + table[62]);
b1[1] = b1[2] + this.rotl(21, b1[1] + this.I(b1[2], b1[3], b1[0]) + seq[9] + table[63]);
b2[0] += b1[0];
b2[1] += b1[1];
b2[2] += b1[2];
b2[3] += b1[3];
}
return b2.buffer;
}
hashToString(hash) {
const dataView = new DataView(hash);
dataView.setUint32(0, dataView.getUint32(0), true);
dataView.setUint32(4, dataView.getUint32(4), true);
dataView.setUint32(8, dataView.getUint32(8), true);
dataView.setUint32(12, dataView.getUint32(12), true);
return [...new Uint32Array(hash)].map((w) => w.toString(16).padStart(8, "0")).join("");
}
F(x, y, z) {
return this.ch(x, y, z);
}
G(x, y, z) {
return this.ch(z, x, y);
}
H(x, y, z) {
return this.parity(x, y, z);
}
I(x, y, z) {
return y ^ (x | ~z);
}
};
// src/util/hash/SHA1.ts
var SHA1 = class extends BaseHashAlgorithm {
padMessage(message) {
const msgLenMod64 = message.length % 64;
let bytesToAdd = 64 - msgLenMod64;
if (msgLenMod64 >= 56) {
bytesToAdd += 64;
}
const padding = new Uint8Array(bytesToAdd);
const paddedMsg = new Uint8Array([...message, ...padding]);
const dataView = new DataView(paddedMsg.buffer);
dataView.setUint8(message.length, 128);
dataView.setBigUint64(paddedMsg.length - 8, BigInt(message.length * 8));
return paddedMsg.buffer;
}
computeHash(paddedMessage) {
const b1 = new Uint32Array(5);
const b2 = new Uint32Array([1732584193, 4023233417, 2562383102, 271733878, 3285377520]);
const seq = new Uint32Array(80);
const dataView = new DataView(paddedMessage);
for (let i = 0; i < paddedMessage.byteLength / 4; i += 16) {
for (let t = 0; t < 16; t++) {
seq[t] = dataView.getUint32(i + t * 4);
}
for (let t = 16; t < 80; t++) {
seq[t] = this.rotl(1, seq[t - 3] ^ seq[t - 8] ^ seq[t - 14] ^ seq[t - 16]);
}
b1[0] = b2[0];
b1[1] = b2[1];
b1[2] = b2[2];
b1[3] = b2[3];
b1[4] = b2[4];
for (let t = 0; t < 80; t++) {
const temp = this.rotl(5, b1[0]) + this.lf(t, b1[1], b1[2], b1[3]) + b1[4] + seq[t] + this.cw(t);
b1[4] = b1[3];
b1[3] = b1[2];
b1[2] = this.rotl(30, b1[1]);
b1[1] = b1[0];
b1[0] = temp;
}
b2[0] += b1[0];
b2[1] += b1[1];
b2[2] += b1[2];
b2[3] += b1[3];
b2[4] += b1[4];
}
return b2.buffer;
}
hashToString(hash) {
return [...new Uint32Array(hash)].map((w) => w.toString(16).padStart(8, "0")).join("");
}
// logical function
lf(t, x, y, z) {
if (t < 20) return this.ch(x, y, z);
else if (t < 40) return this.parity(x, y, z);
else if (t < 60) return this.maj(x, y, z);
else return this.parity(x, y, z);
}
// constant words
cw(t) {
if (t < 20) return 1518500249;
else if (t < 40) return 1859775393;
else if (t < 60) return 2400959708;
else return 3395469782;
}
};
// src/util/hash/SHA256.ts
var SHA256 = class _SHA256 extends BaseHashAlgorithm {
// SHA-256 constants
static k = new Uint32Array([
1116352408,
1899447441,
3049323471,
3921009573,
961987163,
1508970993,
2453635748,
2870763221,
3624381080,
310598401,
607225278,
1426881987,
1925078388,
2162078206,
2614888103,
3248222580,
3835390401,
4022224774,
264347078,
604807628,
770255983,
1249150122,
1555081692,
1996064986,
2554220882,
2821834349,
2952996808,
3210313671,
3336571891,
3584528711,
113926993,
338241895,
666307205,
773529912,
1294757372,
1396182291,
1695183700,
1986661051,
2177026350,
2456956037,
2730485921,
2820302411,
3259730800,
3345764771,
3516065817,
3600352804,
4094571909,
275423344,
430227734,
506948616,
659060556,
883997877,
958139571,
1322822218,
1537002063,
1747873779,
1955562222,
2024104815,
2227730452,
2361852424,
2428436474,
2756734187,
3204031479,
3329325298
]);
padMessage(message) {
const msgLenMod64 = message.length % 64;
let bytesToAdd = 64 - msgLenMod64;
if (msgLenMod64 >= 56) {
bytesToAdd += 64;
}
const padding = new Uint8Array(bytesToAdd);
const paddedMsg = new Uint8Array([...message, ...padding]);
const dataView = new DataView(paddedMsg.buffer);
dataView.setUint8(message.length, 128);
dataView.setBigUint64(paddedMsg.length - 8, BigInt(message.length * 8));
return paddedMsg.buffer;
}
computeHash(paddedMessage) {
const b1 = new Uint32Array(8);
const b2 = new Uint32Array([
1779033703,
3144134277,
1013904242,
2773480762,
1359893119,
2600822924,
528734635,
1541459225
]);
const seq = new Uint32Array(64);
const dataView = new DataView(paddedMessage);
for (let i = 0; i < paddedMessage.byteLength / 4; i += 16) {
for (let t = 0; t < 16; t++) {
seq[t] = dataView.getUint32(i + t * 4);
}
for (let t = 16; t < 64; t++) {
seq[t] = this.smallSigma1(seq[t - 2]) + seq[t - 7] + this.smallSigma0(seq[t - 15]) + seq[t - 16];
}
b1[0] = b2[0];
b1[1] = b2[1];
b1[2] = b2[2];
b1[3] = b2[3];
b1[4] = b2[4];
b1[5] = b2[5];
b1[6] = b2[6];
b1[7] = b2[7];
for (let t = 0; t < 64; t++) {
const temp1 = b1[7] + this.bigSigma1(b1[4]) + this.ch(b1[4], b1[5], b1[6]) + _SHA256.k[t] + seq[t];
const temp2 = this.bigSigma0(b1[0]) + this.maj(b1[0], b1[1], b1[2]);
b1[7] = b1[6];
b1[6] = b1[5];
b1[5] = b1[4];
b1[4] = b1[3] + temp1;
b1[3] = b1[2];
b1[2] = b1[1];
b1[1] = b1[0];
b1[0] = temp1 + temp2;
}
b2[0] += b1[0];
b2[1] += b1[1];
b2[2] += b1[2];
b2[3] += b1[3];
b2[4] += b1[4];
b2[5] += b1[5];
b2[6] += b1[6];
b2[7] += b1[7];
}
return b2.buffer;
}
hashToString(hash) {
return [...new Uint32Array(hash)].map((w) => w.toString(16).padStart(8, "0")).join("");
}
};
// src/util/hash/SHA384.ts
var SHA384 = class _SHA384 extends BaseHashAlgorithm {
// SHA-384 constants
static k = new BigUint64Array([
0x428a2f98d728ae22n,
0x7137449123ef65cdn,
0xb5c0fbcfec4d3b2fn,
0xe9b5dba58189dbbcn,
0x3956c25bf348b538n,
0x59f111f1b605d019n,
0x923f82a4af194f9bn,
0xab1c5ed5da6d8118n,
0xd807aa98a3030242n,
0x12835b0145706fben,
0x243185be4ee4b28cn,
0x550c7dc3d5ffb4e2n,
0x72be5d74f27b896fn,
0x80deb1fe3b1696b1n,
0x9bdc06a725c71235n,
0xc19bf174cf692694n,
0xe49b69c19ef14ad2n,
0xefbe4786384f25e3n,
0x0fc19dc68b8cd5b5n,
0x240ca1cc77ac9c65n,
0x2de92c6f592b0275n,
0x4a7484aa6ea6e483n,
0x5cb0a9dcbd41fbd4n,
0x76f988da831153b5n,
0x983e5152ee66dfabn,
0xa831c66d2db43210n,
0xb00327c898fb213fn,
0xbf597fc7beef0ee4n,
0xc6e00bf33da88fc2n,
0xd5a79147930aa725n,
0x06ca6351e003826fn,
0x142929670a0e6e70n,
0x27b70a8546d22ffcn,
0x2e1b21385c26c926n,
0x4d2c6dfc5ac42aedn,
0x53380d139d95b3dfn,
0x650a73548baf63den,
0x766a0abb3c77b2a8n,
0x81c2c92e47edaee6n,
0x92722c851482353bn,
0xa2bfe8a14cf10364n,
0xa81a664bbc423001n,
0xc24b8b70d0f89791n,
0xc76c51a30654be30n,
0xd192e819d6ef5218n,
0xd69906245565a910n,
0xf40e35855771202an,
0x106aa07032bbd1b8n,
0x19a4c116b8d2d0c8n,
0x1e376c085141ab53n,
0x2748774cdf8eeb99n,
0x34b0bcb5e19b48a8n,
0x391c0cb3c5c95a63n,
0x4ed8aa4ae3418acbn,
0x5b9cca4f7763e373n,
0x682e6ff3d6b2b8a3n,
0x748f82ee5defb2fcn,
0x78a5636f43172f60n,
0x84c87814a1f0ab72n,
0x8cc702081a6439ecn,
0x90befffa23631e28n,
0xa4506cebde82bde9n,
0xbef9a3f7b2c67915n,
0xc67178f2e372532bn,
0xca273eceea26619cn,
0xd186b8c721c0c207n,
0xeada7dd6cde0eb1en,
0xf57d4f7fee6ed178n,
0x06f067aa72176fban,
0x0a637dc5a2c898a6n,
0x113f9804bef90daen,
0x1b710b35131c471bn,
0x28db77f523047d84n,
0x32caab7b40c72493n,
0x3c9ebe0a15c9bebcn,
0x431d67c49c100d4cn,
0x4cc5d4becb3e42b6n,
0x597f299cfc657e2an,
0x5fcb6fab3ad6faecn,
0x6c44198c4a475817n
]);
padMessage(message) {
const msgLenMod128 = message.length % 128;
let bytesToAdd = 128 - msgLenMod128;
if (msgLenMod128 >= 112) {
bytesToAdd += 128;
}
const padding = new Uint8Array(bytesToAdd);
const paddedMsg = new Uint8Array([...message, ...padding]);
const dataView = new DataView(paddedMsg.buffer);
dataView.setUint8(message.length, 128);
dataView.setBigUint64(paddedMsg.length - 8, BigInt(message.length * 8));
return paddedMsg.buffer;
}
computeHash(paddedMessage) {
const b1 = new BigUint64Array(8);
const b2 = new BigUint64Array([
0xcbbb9d5dc1059ed8n,
0x629a292a367cd507n,
0x9159015a3070dd17n,
0x152fecd8f70e5939n,
0x67332667ffc00b31n,
0x8eb44a8768581511n,
0xdb0c2e0d64f98fa7n,
0x47b5481dbefa4fa4n
]);
const seq = new BigUint64Array(80);
const dataView = new DataView(paddedMessage);
for (let i = 0; i < paddedMessage.byteLength / 8; i += 16) {
for (let t = 0; t < 16; t++) {
seq[t] = dataView.getBigUint64(i + t * 8);
}
for (let t = 16; t < 80; t++) {
seq[t] = this.smallSigma1(seq[t - 2]) + seq[t - 7] + this.smallSigma0(seq[t - 15]) + seq[t - 16];
}
b1[0] = b2[0];
b1[1] = b2[1];
b1[2] = b2[2];
b1[3] = b2[3];
b1[4] = b2[4];
b1[5] = b2[5];
b1[6] = b2[6];
b1[7] = b2[7];
for (let t = 0; t < 80; t++) {
const temp1 = b1[7] + this.bigSigma1(b1[4]) + this.ch(b1[4], b1[5], b1[6]) + _SHA384.k[t] + seq[t];
const temp2 = this.bigSigma0(b1[0]) + this.maj(b1[0], b1[1], b1[2]);
b1[7] = b1[6];
b1[6] = b1[5];
b1[5] = b1[4];
b1[4] = b1[3] + temp1;
b1[3] = b1[2];
b1[2] = b1[1];
b1[1] = b1[0];
b1[0] = temp1 + temp2;
}
b2[0] += b1[0];
b2[1] += b1[1];
b2[2] += b1[2];
b2[3] += b1[3];
b2[4] += b1[4];
b2[5] += b1[5];
b2[6] += b1[6];
b2[7] += b1[7];
}
return b2.slice(0, 6).buffer;
}
hashToString(hash) {
return [...new BigUint64Array(hash)].map((w) => w.toString(16).padStart(16, "0")).join("");
}
};
// src/util/hash/SHA512.ts
var SHA512 = class _SHA512 extends BaseHashAlgorithm {
// SHA-512 constants
static k = new BigUint64Array([
0x428a2f98d728ae22n,
0x7137449123ef65cdn,
0xb5c0fbcfec4d3b2fn,
0xe9b5dba58189dbbcn,
0x3956c25bf348b538n,
0x59f111f1b605d019n,
0x923f82a4af194f9bn,
0xab1c5ed5da6d8118n,
0xd807aa98a3030242n,
0x12835b0145706fben,
0x243185be4ee4b28cn,
0x550c7dc3d5ffb4e2n,
0x72be5d74f27b896fn,
0x80deb1fe3b1696b1n,
0x9bdc06a725c71235n,
0xc19bf174cf692694n,
0xe49b69c19ef14ad2n,
0xefbe4786384f25e3n,
0x0fc19dc68b8cd5b5n,
0x240ca1cc77ac9c65n,
0x2de92c6f592b0275n,
0x4a7484aa6ea6e483n,
0x5cb0a9dcbd41fbd4n,
0x76f988da831153b5n,
0x983e5152ee66dfabn,
0xa831c66d2db43210n,
0xb00327c898fb213fn,
0xbf597fc7beef0ee4n,
0xc6e00bf33da88fc2n,
0xd5a79147930aa725n,
0x06ca6351e003826fn,
0x142929670a0e6e70n,
0x27b70a8546d22ffcn,
0x2e1b21385c26c926n,
0x4d2c6dfc5ac42aedn,
0x53380d139d95b3dfn,
0x650a73548baf63den,
0x766a0abb3c77b2a8n,
0x81c2c92e47edaee6n,
0x92722c851482353bn,
0xa2bfe8a14cf10364n,
0xa81a664bbc423001n,
0xc24b8b70d0f89791n,
0xc76c51a30654be30n,
0xd192e819d6ef5218n,
0xd69906245565a910n,
0xf40e35855771202an,
0x106aa07032bbd1b8n,
0x19a4c116b8d2d0c8n,
0x1e376c085141ab53n,
0x2748774cdf8eeb99n,
0x34b0bcb5e19b48a8n,
0x391c0cb3c5c95a63n,
0x4ed8aa4ae3418acbn,
0x5b9cca4f7763e373n,
0x682e6ff3d6b2b8a3n,
0x748f82ee5defb2fcn,
0x78a5636f43172f60n,
0x84c87814a1f0ab72n,
0x8cc702081a6439ecn,
0x90befffa23631e28n,
0xa4506cebde82bde9n,
0xbef9a3f7b2c67915n,
0xc67178f2e372532bn,
0xca273eceea26619cn,
0xd186b8c721c0c207n,
0xeada7dd6cde0eb1en,
0xf57d4f7fee6ed178n,
0x06f067aa72176fban,
0x0a637dc5a2c898a6n,
0x113f9804bef90daen,
0x1b710b35131c471bn,
0x28db77f523047d84n,
0x32caab7b40c72493n,
0x3c9ebe0a15c9bebcn,
0x431d67c49c100d4cn,
0x4cc5d4becb3e42b6n,
0x597f299cfc657e2an,
0x5fcb6fab3ad6faecn,
0x6c44198c4a475817n
]);
padMessage(message) {
const msgLenMod128 = message.length % 128;
let bytesToAdd = 128 - msgLenMod128;
if (msgLenMod128 >= 112) {
bytesToAdd += 128;
}
const padding = new Uint8Array(bytesToAdd);
const paddedMsg = new Uint8Array([...message, ...padding]);
const dataView = new DataView(paddedMsg.buffer);
dataView.setUint8(message.length, 128);
dataView.setBigUint64(paddedMsg.length - 8, BigInt(message.length * 8));
return paddedMsg.buffer;
}
computeHash(paddedMessage) {
const b1 = new BigUint64Array(8);
const b2 = new BigUint64Array([
0x6a09e667f3bcc908n,
0xbb67ae8584caa73bn,
0x3c6ef372fe94f82bn,
0xa54ff53a5f1d36f1n,
0x510e527fade682d1n,
0x9b05688c2b3e6c1fn,
0x1f83d9abfb41bd6bn,
0x5be0cd19137e2179n
]);
const seq = new BigUint64Array(80);
const dataView = new DataView(paddedMessage);
for (let i = 0; i < paddedMessage.byteLength / 8; i += 16) {
for (let t = 0; t < 16; t++) {
seq[t] = dataView.getBigUint64(i + t * 8);
}
for (let t = 16; t < 80; t++) {
seq[t] = this.smallSigma1(seq[t - 2]) + seq[t - 7] + this.smallSigma0(seq[t - 15]) + seq[t - 16];
}
b1[0] = b2[0];
b1[1] = b2[1];
b1[2] = b2[2];
b1[3] = b2[3];
b1[4] = b2[4];
b1[5] = b2[5];
b1[6] = b2[6];
b1[7] = b2[7];
for (let t = 0; t < 80; t++) {
const temp1 = b1[7] + this.bigSigma1(b1[4]) + this.ch(b1[4], b1[5], b1[6]) + _SHA512.k[t] + seq[t];
const temp2 = this.bigSigma0(b1[0]) + this.maj(b1[0], b1[1], b1[2]);
b1[7] = b1[6];
b1[6] = b1[5];
b1[5] = b1[4];
b1[4] = b1[3] + temp1;
b1[3] = b1[2];
b1[2] = b1[1];
b1[1] = b1[0];
b1[0] = temp1 + temp2;
}
b2[0] += b1[0];
b2[1] += b1[1];
b2[2] += b1[2];
b2[3] += b1[3];
b2[4] += b1[4];
b2[5] += b1[5];
b2[6] += b1[6];
b2[7] += b1[7];
}
return b2.buffer;
}
hashToString(hash) {
return [...new BigUint64Array(hash)].map((w) => w.toString(16).padStart(16, "0")).join("");
}
};
// src/util/hash.ts
var md5 = new MD5();
var sha1 = new SHA1();
var sha256 = new SHA256();
var sha384 = new SHA384();
var sha512 = new SHA512();
// src/stateMachine/intrinsicFunctions/StatesHash.ts
var StatesHash = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.Hash",
exactArgs: 2,
arguments: [
{
allowedTypes: ["string"]
},
{
allowedTypes: ["string"]
}
]
};
}
execute(str, algorithm) {
if (str.length > 1e4) {
throw new StatesRuntimeError(
`Intrinsic function ${this.funcDefinition.name} cannot hash a string with more than 10,000 characters`
);
}
const algorithms = ["MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512"];
const supportedAlgorithms = algorithms.join(", ");
if (!algorithms.includes(algorithm)) {
throw new StatesRuntimeError(
`Unsupported hash algorithm '${algorithm}' provided to intrinsic function ${this.funcDefinition.name}. The supported algorithms are: ${supportedAlgorithms}`
);
}
switch (algorithm) {
case "MD5":
return md5.getDigest(str);
case "SHA-1":
return sha1.getDigest(str);
case "SHA-256":
return sha256.getDigest(str);
case "SHA-384":
return sha384.getDigest(str);
case "SHA-512":
return sha512.getDigest(str);
}
}
};
// src/stateMachine/intrinsicFunctions/StatesJsonMerge.ts
var StatesJsonMerge = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.JsonMerge",
exactArgs: 3,
arguments: [
{
allowedTypes: ["object"]
},
{
allowedTypes: ["object"]
},
{
allowedTypes: ["boolean"]
}
]
};
}
execute(obj1, obj2, isDeepMerge) {
if (isDeepMerge) {
throw new StatesRuntimeError(
`Deep merge option is not supported in ${this.funcDefinition.name}. Third argument must be set to false instead of true`
);
}
return Object.assign(obj1, obj2);
}
};
// src/stateMachine/intrinsicFunctions/StatesMathRandom.ts
var StatesMathRandom = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.MathRandom",
minArgs: 2,
arguments: [
{
allowedTypes: ["number"],
constraints: ["INTEGER"]
},
{
allowedTypes: ["number"],
constraints: ["INTEGER"]
},
{
allowedTypes: ["number"]
}
]
};
}
execute(min, max, seed) {
const [s1, s2, s3, s4] = cyrb128((seed ?? Date.now()).toString());
const rng = sfc32(s1, s2, s3, s4);
return getRandomNumber(min, max, rng);
}
};
// src/stateMachine/intrinsicFunctions/StatesMathAdd.ts
var StatesMathAdd = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.MathAdd",
exactArgs: 2,
arguments: [
{
allowedTypes: ["number"]
},
{
allowedTypes: ["number"]
}
]
};
}
execute(num1, num2) {
return num1 + num2;
}
};
// src/stateMachine/intrinsicFunctions/StatesStringSplit.ts
var StatesStringSplit = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.StringSplit",
exactArgs: 2,
arguments: [
{
allowedTypes: ["string"]
},
{
allowedTypes: ["string"]
}
]
};
}
execute(str, separator) {
return str.split(separator);
}
};
// src/stateMachine/intrinsicFunctions/StatesUUID.ts
var StatesUUID = class extends BaseIntrinsicFunction {
funcDefinition;
constructor() {
super();
this.funcDefinition = {
name: "States.UUID"
};
}
execute() {
const octets = new Uint8Array(16);
for (let i = 0; i < octets.length; i++) {
octets[i] = getRandomNumber(0, 255);
}
octets[6] = octets[6] & 127;
octets[6] = octets[6] | 64;
octets[6] = octets[6] & 223;
octets[6] = octets[6] & 239;
octets[8] = octets[8] | 128;
octets[8] = octets[8] & 191;
const b0 = byteToHex(octets[0]);
const b1 = byteToHex(octets[1]);
const b2 = byteToHex(octets[2]);
const b3 = byteToHex(octets[3]);
const b4 = byteToHex(octets[4]);
const b5 = byteToHex(octets[5]);
const b6 = byteToHex(octets[6]);
const b7 = byteToHex(octets[7]);
const b8 = byteToHex(octets[8]);
const b9 = byteToHex(octets[9]);
const b10 = byteToHex(octets[10]);
const b11 = byteToHex(octets[11]);
const b12 = byteToHex(octets[12]);
const b13 = byteToHex(octets[13]);
const b14 = byteToHex(octets[14]);
const b15 = byteToHex(octets[15]);
return `${b0}${b1}${b2}${b3}-${b4}${b5}-${b6}${b7}-${b8}${b9}-${b10}${b11}${b12}${b13}${b14}${b15}`;
}
};
// src/stateMachine/IntrinsicFunctionEvaluation.ts
var functions = {
"States.Format": new StatesFormat(),
"States.StringToJson": new StatesStringToJson(),
"States.JsonToString": new StatesJsonToString(),
"States.Array": new StatesArray(),
"States.ArrayPartition": new StatesArrayPartition(),
"States.ArrayContains": new StatesArrayContains(),
"States.ArrayRange": new StatesArrayRange(),
"States.ArrayGetItem": new StatesArrayGetItem(),
"States.ArrayLength": new StatesArrayLength(),
"States.ArrayUnique": new StatesArrayUnique(),
"States.Base64Encode": new StatesBase64Encode(),
"States.Base64Decode": new StatesBase64Decode(),
"States.Hash": new StatesHash(),
"States.JsonMerge": new StatesJsonMerge(),
"States.MathRandom": new StatesMathRandom(),
"States.MathAdd": new StatesMathAdd(),
"States.StringSplit": new StatesStringSplit(),
"States.UUID": new StatesUUID()
};
function evaluateIntrinsicFunction(intrinsicFunction, input, context) {
const openingParensIdx = intrinsicFunction.indexOf("(");
const funcName = intrinsicFunction.slice(0, openingParensIdx);
const funcArgs = intrinsicFunction.slice(openingParensIdx + 1, intrinsicFunction.length - 1);
const splitArgs = [];
let partialArg = "";
let parensCount = 0;
let apostropheCount = 0;
for (let