Skip to content

Commit

Permalink
fix for parse network
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Mar 3, 2024
1 parent c3e3396 commit e4dc7be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/parseFilename.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from "bun:test";
import { parseNetwork } from "./parseFilename.js";

test("parseNetwork", () => {
expect(parseNetwork("mainnet.eth.streamingfast.io:443")).toBe("mainnet.eth.streamingfast.io");
expect(parseNetwork("http://mainnet.eth.streamingfast.io:443")).toBe("mainnet.eth.streamingfast.io");
expect(parseNetwork("https://mainnet.eth.streamingfast.io:443")).toBe("mainnet.eth.streamingfast.io");
expect(parseNetwork("http://eth-sfst79.mar.eosn.io:9000")).toBe("eth-sfst79.mar.eosn.io");
})
36 changes: 21 additions & 15 deletions src/parseFilename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import fs from "fs";
import { CSVRunOptions } from "../bin/cli.js";

export function parseFilename(moduleHash: string, options: CSVRunOptions) {
// user provided filename
if ( options.filename ) {
const name = path.parse(options.filename).name;
const dirname = path.dirname(options.filename);
// create directory if it doesn't exist
if ( !fs.existsSync(dirname) ) fs.mkdirSync(dirname, { recursive: true });
const cursorFile = `${name}.cursor`;
const clockFile = `${name}.clock`;
const sessionFile = `${name}.session`;
return { name, cursorFile, clockFile, sessionFile };
}
// auto-generate filename (<network>-<moduleHash>-<moduleName>.csv)
const network = options.substreamsEndpoint.split(":")[0];
const name = `${network}-${moduleHash}-${options.moduleName}`
// user provided filename
if ( options.filename ) {
const name = path.parse(options.filename).name;
const dirname = path.dirname(options.filename);
// create directory if it doesn't exist
if ( !fs.existsSync(dirname) ) fs.mkdirSync(dirname, { recursive: true });
const cursorFile = `${name}.cursor`;
const clockFile = `${name}.clock`;
const sessionFile = `${name}.session`;
return { name, cursorFile, clockFile, sessionFile };
}
}

// auto-generate filename (<network>-<moduleHash>-<moduleName>.csv)
const network = parseNetwork(options.substreamsEndpoint);
const name = `${network}-${moduleHash}-${options.moduleName}`
const cursorFile = `${name}.cursor`;
const clockFile = `${name}.clock`;
const sessionFile = `${name}.session`;
return { name, cursorFile, clockFile, sessionFile };
}

export function parseNetwork(endpoint: string) {
if ( !endpoint.startsWith("http") ) endpoint = `http://${endpoint}`
return new URL(endpoint).hostname;
}

0 comments on commit e4dc7be

Please sign in to comment.