envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
723 lines (639 loc) • 15.8 kB
JavaScript
import packageJson from '../package.json' with { type: 'json' }
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
import * as Stdlib_BigInt from "@rescript/runtime/lib/es6/Stdlib_BigInt.js";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as Stdlib_String from "@rescript/runtime/lib/es6/Stdlib_String.js";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as Stdlib_Promise from "@rescript/runtime/lib/es6/Stdlib_Promise.js";
import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js";
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
function delay(milliseconds) {
return new Promise((resolve, param) => {
setTimeout(() => resolve(), milliseconds);
});
}
let $$Object = {};
let $$Error = {};
function mapNone(opt, val) {
if (opt !== undefined) {
return;
} else {
return Primitive_option.some(val);
}
}
function catchToNone(unsafeFunc) {
try {
return Primitive_option.some(unsafeFunc());
} catch (exn) {
return;
}
}
function flatten(opt) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
}
}
function getExn(opt, message) {
if (opt !== undefined) {
return Primitive_option.valFromOption(opt);
} else {
return Stdlib_JsError.throwWithMessage(message);
}
}
let Option = {
mapNone: mapNone,
catchToNone: catchToNone,
flatten: flatten,
getExn: getExn
};
function getOrInsertEmptyDict(dict, key) {
let d = dict[key];
if (d !== undefined) {
return d;
}
let d$1 = {};
dict[key] = d$1;
return d$1;
}
function push(dict, key, value) {
let arr = dict[key];
if (arr !== undefined) {
arr.push(value);
} else {
dict[key] = [value];
}
}
function pushMany(dict, key, values) {
let arr = dict[key];
if (arr !== undefined) {
arr.push(...values);
} else {
dict[key] = values;
}
}
let merge = ((dictA, dictB) => ({...dictA, ...dictB}));
let mapValues = ((dict, f) => {
var target = {}, i;
for (i in dict) {
target[i] = f(dict[i]);
}
return target;
});
let filterMapValues = ((dict, f) => {
var target = {}, i, v;
for (i in dict) {
v = f(dict[i]);
if (v !== undefined) {
target[i] = v;
}
}
return target;
});
let mapValuesToArray = ((dict, f) => {
var target = [], i;
for (i in dict) {
target.push(f(dict[i]));
}
return target;
});
let forEach = ((dict, f) => {
for (var i in dict) {
f(dict[i]);
}
});
let forEachWithKey = ((dict, f) => {
for (var i in dict) {
f(dict[i], i);
}
});
let size = ((dict) => {
var size = 0, i;
for (i in dict) {
size++;
}
return size;
});
let isEmpty = ((dict) => {
for (var _ in dict) {
return false
}
return true
});
let deleteInPlace = ((dict, key) => {
delete dict[key];
});
let unsafeDeleteUndefinedFieldsInPlace = ((dict) => {
for (var key in dict) {
if (dict[key] === undefined) {
delete dict[key];
}
}
});
let updateImmutable = ((dict, key, value) => ({...dict, [key]: value}));
let shallowCopy = ((dict) => ({...dict}));
let incrementByInt = ((dict, key) => {
dict[key]++
});
let Dict = {
$$delete: Stdlib_Dict.$$delete,
getOrInsertEmptyDict: getOrInsertEmptyDict,
push: push,
pushMany: pushMany,
merge: merge,
mapValues: mapValues,
filterMapValues: filterMapValues,
mapValuesToArray: mapValuesToArray,
forEach: forEach,
forEachWithKey: forEachWithKey,
size: size,
isEmpty: isEmpty,
deleteInPlace: deleteInPlace,
unsafeDeleteUndefinedFieldsInPlace: unsafeDeleteUndefinedFieldsInPlace,
updateImmutable: updateImmutable,
shallowCopy: shallowCopy,
incrementByInt: incrementByInt
};
function minOptInt(a, b) {
if (a === undefined) {
if (b !== undefined) {
return Primitive_option.some(Primitive_option.valFromOption(b));
} else {
return;
}
}
let a$1 = Primitive_option.valFromOption(a);
if (b === undefined) {
return Primitive_option.some(a$1);
}
let b$1 = Primitive_option.valFromOption(b);
return Primitive_option.some(Primitive_object.lessthan(a$1, b$1) ? a$1 : b$1);
}
let $$Math = {
minOptInt: minOptInt
};
let UnsafeIntOperators = {};
let immutableEmpty = [];
function mergeSorted(f, xs, ys) {
if (xs.length === 0) {
return ys;
}
if (ys.length === 0) {
return xs;
}
let n = xs.length + ys.length | 0;
let result = Array(n);
let loop = (_i, _j, _k) => {
while (true) {
let k = _k;
let j = _j;
let i = _i;
if (i < xs.length && j < ys.length) {
if (f(xs[i], ys[j])) {
result[k] = xs[i];
_k = k + 1 | 0;
_i = i + 1 | 0;
continue;
}
result[k] = ys[j];
_k = k + 1 | 0;
_j = j + 1 | 0;
continue;
}
if (i < xs.length) {
result[k] = xs[i];
_k = k + 1 | 0;
_i = i + 1 | 0;
continue;
}
if (j >= ys.length) {
return;
}
result[k] = ys[j];
_k = k + 1 | 0;
_j = j + 1 | 0;
continue;
};
};
loop(0, 0, 0);
return result;
}
let clearInPlace = ((arr) => {
arr.length = 0
});
function setIndexImmutable(arr, index, value) {
let shallowCopy = arr.slice();
shallowCopy[index] = value;
return shallowCopy;
}
function transposeResults(results) {
let _index = 0;
let output = Array(results.length);
while (true) {
let index = _index;
if (index >= results.length) {
return {
TAG: "Ok",
_0: output
};
}
let value = results[index];
if (value.TAG !== "Ok") {
return value;
}
output[index] = value._0;
_index = index + 1 | 0;
continue;
};
}
function includes(arr, val) {
return Stdlib_Option.isSome(arr.find(item => Primitive_object.equal(item, val)));
}
function isEmpty$1(arr) {
return arr.length === 0;
}
function notEmpty(arr) {
return arr.length !== 0;
}
async function awaitEach(arr, fn) {
for (let i = 0, i_finish = arr.length; i < i_finish; ++i) {
let item = arr[i];
await fn(item);
}
}
function removeAtIndex(array, index) {
if (index < 0) {
return array.slice();
} else {
return array.slice(0, index).concat(array.slice(index + 1 | 0));
}
}
function last(arr) {
return arr[arr.length - 1 | 0];
}
function first(arr) {
return arr[0];
}
function lastUnsafe(arr) {
return arr[arr.length - 1 | 0];
}
function firstUnsafe(arr) {
return arr[0];
}
function findReverseWithIndex(arr, fn) {
let _index = arr.length - 1 | 0;
while (true) {
let index = _index;
if (index < 0) {
return;
}
let item = arr[index];
if (fn(item)) {
return [
item,
index
];
}
_index = index - 1 | 0;
continue;
};
}
function interleave(arr, separator) {
let interleaved = [];
arr.forEach((v, i) => {
interleaved.push(v);
if (i < (arr.length - 1 | 0)) {
interleaved.push(separator);
return;
}
});
return interleaved;
}
let fromAsyncIterator = (async (iterator) => {
const result = [];
for await (const item of iterator) {
result.push(item);
}
return result;
});
let $$Array$1 = {
make: Stdlib_Array.make,
fromInitializer: Stdlib_Array.fromInitializer,
equal: Stdlib_Array.equal,
compare: Stdlib_Array.compare,
indexOfOpt: Stdlib_Array.indexOfOpt,
lastIndexOfOpt: Stdlib_Array.lastIndexOfOpt,
reduce: Stdlib_Array.reduce,
reduceWithIndex: Stdlib_Array.reduceWithIndex,
reduceRight: Stdlib_Array.reduceRight,
reduceRightWithIndex: Stdlib_Array.reduceRightWithIndex,
findIndexOpt: Stdlib_Array.findIndexOpt,
findLastIndexOpt: Stdlib_Array.findLastIndexOpt,
filterMap: Stdlib_Array.filterMap,
filterMapWithIndex: Stdlib_Array.filterMapWithIndex,
keepSome: Stdlib_Array.keepSome,
toShuffled: Stdlib_Array.toShuffled,
shuffle: Stdlib_Array.shuffle,
findMap: Stdlib_Array.findMap,
zip: Stdlib_Array.zip,
zipBy: Stdlib_Array.zipBy,
partition: Stdlib_Array.partition,
unzip: Stdlib_Array.unzip,
immutableEmpty: immutableEmpty,
mergeSorted: mergeSorted,
clearInPlace: clearInPlace,
setIndexImmutable: setIndexImmutable,
transposeResults: transposeResults,
includes: includes,
isEmpty: isEmpty$1,
notEmpty: notEmpty,
awaitEach: awaitEach,
removeAtIndex: removeAtIndex,
last: last,
first: first,
lastUnsafe: lastUnsafe,
firstUnsafe: firstUnsafe,
findReverseWithIndex: findReverseWithIndex,
interleave: interleave,
fromAsyncIterator: fromAsyncIterator
};
function capitalize(str) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}
let $$String = {
charCodeAt: Stdlib_String.charCodeAt,
indexOfOpt: Stdlib_String.indexOfOpt,
lastIndexOfOpt: Stdlib_String.lastIndexOfOpt,
searchOpt: Stdlib_String.searchOpt,
isEmpty: Stdlib_String.isEmpty,
capitalize: capitalize
};
function getHostFromUrl(url) {
let regex = /https?:\/\/([^\/?]+).*/;
let result = regex.exec(url);
if (result == null) {
return;
}
let match = result.slice(1)[0];
if (match === undefined) {
return;
}
let host = Primitive_option.valFromOption(match);
if (host !== undefined) {
return host;
}
}
let Url = {
getHostFromUrl: getHostFromUrl
};
function forEach$1(result, fn) {
if (result.TAG === "Ok") {
fn(result._0);
}
return result;
}
let Result = {
forEach: forEach$1
};
function unwrapResultExn(res) {
if (res.TAG === "Ok") {
return res._0;
}
throw res._0;
}
let variantTag = S$RescriptSchema.union([
S$RescriptSchema.string,
S$RescriptSchema.object(s => s.f("TAG", S$RescriptSchema.string))
]);
let dbDate = S$RescriptSchema.preprocess(S$RescriptSchema.json(false), param => ({
s: date => date.toISOString()
}));
let clickHouseDate = S$RescriptSchema.preprocess(S$RescriptSchema.json(false), param => ({
s: date => date.getTime()
}));
function coerceToJsonPgType(schema) {
return S$RescriptSchema.preprocess(schema, s => {
let match = s.schema.t;
if (typeof match === "object") {
return {};
}
switch (match) {
case "unknown" :
return {
s: param => ("null")
};
case "boolean" :
return {
s: unknown => {
if (unknown === false) {
return "false";
} else if (unknown === true) {
return "true";
} else {
return unknown;
}
}
};
default:
return {};
}
});
}
let Schema = {
variantTag: variantTag,
dbDate: dbDate,
clickHouseDate: clickHouseDate,
coerceToJsonPgType: coerceToJsonPgType
};
function getVariantsTags(variants) {
return variants.map(variant => S$RescriptSchema.parseOrThrow(variant, variantTag));
}
function addMany(set, values) {
values.forEach(value => {
set.add(value);
});
}
let immutableAdd = ((set, value) => {
return new Set([...set, value])
});
let $$Set = {
addMany: addMany,
immutableAdd: immutableAdd
};
function fromArray(pairs) {
return Object.fromEntries(pairs);
}
let Record = {
fromArray: fromArray
};
function memoize(fn) {
let cache = new WeakMap();
return key => {
let v = cache.get(key);
if (v !== undefined) {
return Primitive_option.valFromOption(v);
}
let v$1 = fn(key);
cache.set(key, v$1);
return v$1;
};
}
let $$WeakMap$1 = {
memoize: memoize
};
let $$Map = {};
let $$Proxy = {};
function fail(name) {
return Stdlib_JsError.throwWithMessage(`Failed to get hash for ` + name + `. If you're using a custom Sury schema make it based on the string type with a decoder: const myTypeSchema = S.transform(S.string, undefined, (yourType) => yourType.toString())`);
}
function makeOrThrow(any) {
let match = typeof any;
if (match === "function" || match === "symbol") {
return any.toString();
}
if (match === "boolean") {
if (any) {
return "true";
} else {
return "false";
}
}
if (match === "string") {
return `"` + any + `"`;
}
if (match !== "object") {
if (match === "undefined") {
return "null";
} else if (match === "number") {
return any.toString();
} else {
return `"` + any.toString() + `"`;
}
}
if (any === null) {
return "null";
}
if (Array.isArray(any)) {
let hash = "[";
for (let i = 0, i_finish = any.length; i < i_finish; ++i) {
if (i !== 0) {
hash = hash + ",";
}
hash = hash + makeOrThrow(any[i]);
}
return hash + "]";
}
let constructor = any["constructor"];
if (constructor !== Object) {
if (constructor.name === "BigNumber") {
return `"` + any.toString() + `"`;
} else {
return fail(constructor.name);
}
}
let hash$1 = "{";
let keys = Object.keys(any).toSorted(Primitive_string.compare);
let isFirst = true;
for (let i$1 = 0, i_finish$1 = keys.length; i$1 < i_finish$1; ++i$1) {
let key = keys[i$1];
let value = any[key];
if (value !== undefined) {
if (isFirst) {
isFirst = false;
} else {
hash$1 = hash$1 + ",";
}
hash$1 = hash$1 + (`"` + key + `":` + makeOrThrow(any[key]));
}
}
return hash$1 + "}";
}
let Hash = {
fail: fail,
makeOrThrow: makeOrThrow
};
function prettifyExn(exn) {
let e = Primitive_exceptions.internalToException(exn);
if (e.RE_EXN_ID === "JsExn") {
return e._1;
} else {
return e;
}
}
let value;
try {
value = S$RescriptSchema.parseJsonOrThrow(packageJson, S$RescriptSchema.schema(s => ({
version: s.m(S$RescriptSchema.string)
})));
} catch (raw_error) {
let error = Primitive_exceptions.internalToException(raw_error);
if (error.RE_EXN_ID === S$RescriptSchema.Raised) {
value = Stdlib_JsError.throwWithMessage(`Failed to get package.json in envio package: ` + S$RescriptSchema.$$Error.message(error._1));
} else {
throw error;
}
}
let EnvioPackage = {
value: value
};
let arrayToStringArray = ((arr) => {
const res = new Array(arr.length);
for (let i = 0; i < arr.length; i++) {
res[i] = arr[i].toString();
}
return res;
});
let schema = S$RescriptSchema.transform(S$RescriptSchema.setName(S$RescriptSchema.string, "BigInt"), s => ({
p: string => {
let bigInt = Stdlib_BigInt.fromString(string);
if (bigInt !== undefined) {
return bigInt;
} else {
return s.fail("The string is not valid BigInt", undefined);
}
},
s: bigint => bigint.toString()
}));
let $$BigInt = {
arrayToStringArray: arrayToStringArray,
schema: schema
};
function silentCatch(promise) {
return Stdlib_Promise.$$catch(promise, param => {});
}
let isCatchable = (value => value && typeof value.catch === 'function');
let $$Promise$1 = {
silentCatch: silentCatch,
isCatchable: isCatchable
};
export {
delay,
$$Object,
$$Error,
Option,
Dict,
$$Math,
UnsafeIntOperators,
$$Array$1 as $$Array,
$$String,
Url,
Result,
unwrapResultExn,
Schema,
getVariantsTags,
$$Set,
Record,
$$WeakMap$1 as $$WeakMap,
$$Map,
$$Proxy,
Hash,
prettifyExn,
EnvioPackage,
$$BigInt,
$$Promise$1 as $$Promise,
}
/* fromAsyncIterator Not a pure module */