envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
202 lines (179 loc) • 5.6 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Nodefs from "node:fs";
import * as Nodeurl from "node:url";
import * as Nodepath from "node:path";
import * as Stdlib_Null from "@rescript/runtime/lib/es6/Stdlib_Null.js";
import * as Nodemodule from "node:module";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as Nodechild_process from "node:child_process";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
function _keepFs(prim) {
return Nodefs.existsSync(prim);
}
function _keepCp(prim0, prim1) {
return Nodechild_process.execSync(prim0, prim1);
}
let callRequire = ((req, id) => req(id));
let envioPackageDir = Nodepath.dirname(Nodepath.dirname(Nodeurl.fileURLToPath(import.meta.url)));
let loadDevAddon = (function(req, envioDir) {
var cp = Nodechild_process;
var path = Nodepath;
var fs = Nodefs;
var repoRoot = null;
var dir = path.resolve(envioDir);
for (var i = 0; i < 10; i++) {
dir = path.dirname(dir);
if (dir === path.dirname(dir)) break;
if (fs.existsSync(path.join(dir, "packages", "cli", "Cargo.toml"))) {
repoRoot = dir;
break;
}
}
if (!repoRoot) {
var result;
try {
result = cp.execSync("pnpm list envio --json", {
encoding: "utf8", timeout: 10000,
stdio: ["ignore", "pipe", "ignore"],
});
} catch (e) { return null; }
var parsed;
try {
var s = result.trim();
if (s.startsWith(",")) s = s.slice(1);
if (s.endsWith(",")) s = s.slice(0, -1);
parsed = JSON.parse(s);
} catch (e) { return null; }
var ver;
try {
var pkg = Array.isArray(parsed) ? parsed[0] : parsed;
ver = (pkg.dependencies || pkg.devDependencies || {}).envio.version;
} catch (e) { return null; }
if (!ver || !ver.startsWith("file:")) return null;
repoRoot = path.resolve(path.resolve(ver.replace("file:", "")), "..", "..");
if (!fs.existsSync(path.join(repoRoot, "packages", "cli", "Cargo.toml"))) return null;
}
var cliDir = path.join(repoRoot, "packages", "cli");
try {
cp.execSync("cargo build --lib", { cwd: cliDir, stdio: "inherit" });
} catch (e) {
throw new Error("Failed to build envio NAPI addon. Run 'cargo build --lib' in " + cliDir + " manually.");
}
var libName = process.platform === "darwin" ? "libenvio.dylib"
: process.platform === "win32" ? "envio.dll" : "libenvio.so";
var srcPath = path.join(repoRoot, "target", "debug", libName);
var nodePath = path.join(repoRoot, "target", "debug", "envio.node");
if (!fs.existsSync(srcPath)) {
throw new Error("cargo build succeeded but " + srcPath + " not found.");
}
if (!fs.existsSync(nodePath) || fs.statSync(nodePath).mtimeMs < fs.statSync(srcPath).mtimeMs) {
fs.copyFileSync(srcPath, nodePath);
}
return req(nodePath);
});
let rethrow = (function(e) { throw e });
function loadAddon() {
let req = Nodemodule.createRequire(import.meta.url);
let match = process.platform;
let match$1 = process.arch;
let candidates;
switch (match) {
case "darwin" :
switch (match$1) {
case "arm64" :
candidates = [`envio-darwin-arm64`];
break;
case "x64" :
candidates = [`envio-darwin-x64`];
break;
default:
candidates = [];
}
break;
case "linux" :
switch (match$1) {
case "arm64" :
candidates = [`envio-linux-arm64`];
break;
case "x64" :
candidates = [
`envio-linux-x64`,
`envio-linux-x64-musl`
];
break;
default:
candidates = [];
}
break;
default:
candidates = [];
}
let tryRequire = _i => {
while (true) {
let i = _i;
let pkg = candidates[i];
if (pkg === undefined) {
return;
}
try {
return callRequire(req, pkg);
} catch (raw_exn) {
let exn = Primitive_exceptions.internalToException(raw_exn);
let e = Primitive_exceptions.internalToException(exn);
if (e.RE_EXN_ID === "JsExn") {
let e$1 = e._1;
if (e$1.code !== "MODULE_NOT_FOUND") {
return rethrow(e$1);
}
_i = i + 1 | 0;
continue;
}
throw exn;
}
};
};
let addon = tryRequire(0);
if (addon !== undefined) {
return addon;
}
let addon$1 = loadDevAddon(req, envioPackageDir);
if (addon$1 !== undefined) {
return addon$1;
}
let host = process.platform + `-` + process.arch;
return Stdlib_JsError.throwWithMessage(candidates.length === 0 ? `envio doesn't support ` + host + `. Supported: linux-x64 (glibc/musl), linux-arm64, darwin-x64, darwin-arm64.` : `Couldn't load the envio native addon for ` + host + `. Reinstall envio (ensure optional dependencies aren't skipped).`);
}
let addonRef = {
contents: undefined
};
function getAddon() {
let a = addonRef.contents;
if (a !== undefined) {
return a;
}
let a$1 = loadAddon();
addonRef.contents = a$1;
return a$1;
}
function getConfigJson(configPath, directory) {
let addon = getAddon();
return addon.getConfigJson(Stdlib_Null.fromOption(configPath), Stdlib_Null.fromOption(directory));
}
function runCli(args) {
let addon = getAddon();
return addon.runCli(args, envioPackageDir);
}
export {
_keepFs,
_keepCp,
callRequire,
envioPackageDir,
loadDevAddon,
rethrow,
loadAddon,
addonRef,
getAddon,
getConfigJson,
runCli,
}
/* envioPackageDir Not a pure module */