Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proxy support from *_PROXY environment variables #8

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { OutPointVec } from "./extensions.esm";
import { version } from "./package.json";
import { Command } from "commander";
import { createReadStream, writeFileSync } from "fs";
import { argv, stdin } from "process";
import { stdin } from "process";
import { default as nodeFetch } from "node-fetch";
import { ProxyAgent } from "proxy-agent";

function outPointsUnpacker(data) {
const vec = new OutPointVec(data);
Expand All @@ -12,7 +14,7 @@ function outPointsUnpacker(data) {
const item = vec.indexAt(i);
results.push({
tx_hash: new Reader(item.getTxHash().raw()).serializeJson(),
index: "0x" + BigInt(item.getIndex().toLittleEndianUint32()).toString(16)
index: "0x" + BigInt(item.getIndex().toLittleEndianUint32()).toString(16),
});
}
return results;
Expand All @@ -25,27 +27,36 @@ program
.option("-t, --tx <tx>", "TX file to read, use STDIN if omitted")
.option(
"-x, --tx-hash <tx-hash>",
"TX hash to load, when both present, this take precedence over --tx"
"TX hash to load, when both present, this take precedence over --tx",
)
.option(
"-o, --output <output file>",
"Output file containing dumped tx",
"dump.json"
"dump.json",
)
.option("-p, --pretty-print", "Pretty print result file");
program.parse(argv);
.option("-p, --pretty-print", "Pretty print result file")
.option("-n, --no-proxy", "Disable proxies");
program.parse();
const options = program.opts();

const run = async () => {
const rpc = new RPC(program.rpc, {}, global.fetch);
let fetchImpl = nodeFetch;
if (options.proxy) {
const agent = new ProxyAgent();
fetchImpl = (resource, options) => {
return nodeFetch(resource, Object.assign({}, options, { agent }));
};
}
const rpc = new RPC(options.rpc, {}, fetchImpl);

let tx;
if (program.txHash) {
tx = (await rpc.get_transaction(program.txHash)).transaction;
if (options.txHash) {
tx = (await rpc.get_transaction(options.txHash)).transaction;
delete tx.hash;
} else {
let s = stdin;
if (program.tx) {
s = createReadStream(program.tx);
if (options.tx) {
s = createReadStream(options.tx);
}
let result = "";
for await (const chunk of s) {
Expand All @@ -55,13 +66,13 @@ const run = async () => {
}

const dumper = new TransactionDumper(rpc, {
depGroupUnpacker: outPointsUnpacker
depGroupUnpacker: outPointsUnpacker,
});
let data = await dumper.dump(tx);
if (program.prettyPrint) {
if (options.prettyPrint) {
data = JSON.stringify(JSON.parse(data), null, 2);
}
writeFileSync(program.output, data);
writeFileSync(options.output, data);
};

run();
Loading