{"size": 1706, "ext": "js", "max_stars_repo_path": "src/utils.js", "max_stars_repo_name": "namebasehq/nodejs-skynet", "max_stars_repo_stars_event_min_datetime": "2020-02-18T19:33:07.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-14T15:47:46.000Z", "max_issues_repo_path": "src/utils.js", "max_issues_repo_name": "namebasehq/nodejs-skynet", "max_issues_repo_issues_event_min_datetime": "2020-02-13T20:00:10.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-15T19:03:43.000Z", "max_forks_repo_path": "src/utils.js", "max_forks_repo_name": "namebasehq/nodejs-skynet", "max_forks_repo_forks_event_min_datetime": "2020-02-19T02:13:21.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-24T15:28:02.000Z"}
{"max_stars_count": 34, "max_issues_count": 51, "max_forks_count": 16, "avg_line_length": 21.325, "max_line_length": 126, "alphanum_fraction": 0.6881594373}
"use strict";

const path = require("path");
const fs = require("fs");
const urljoin = require("url-join");

/**
 * The default URL of the Skynet portal to use in the absence of configuration.
 */
const defaultSkynetPortalUrl = "https://siasky.net";

/**
 * The URI prefix for Skynet.
 */
const uriSkynetPrefix = "sia://";

function defaultOptions(endpointPath) {
  return {
    portalUrl: defaultPortalUrl(),
    endpointPath: endpointPath,

    APIKey: "",
    customUserAgent: "",
  };
}

// TODO: This will be smarter. See
// https://github.com/NebulousLabs/skynet-docs/issues/21.
/**
 * Selects the default portal URL to use when initializing a client. May involve network queries to several candidate portals.
 */
function defaultPortalUrl() {
  return defaultSkynetPortalUrl;
}

/**
 * Properly joins paths together to create a URL. Takes a variable number of
 * arguments.
 */
function makeUrl() {
  let args = Array.from(arguments);
  return args.reduce(function (acc, cur) {
    return urljoin(acc, cur);
  });
}

function walkDirectory(filepath, out) {
  let files = [];
  if (!fs.existsSync(filepath)) {
    return files;
  }

  for (const subpath of fs.readdirSync(filepath)) {
    const fullpath = path.join(filepath, subpath);
    if (fs.statSync(fullpath).isDirectory()) {
      files = files.concat(walkDirectory(fullpath, out));
      continue;
    }
    files.push(fullpath);
  }
  return files;
}

function trimSiaPrefix(str) {
  if (str.startsWith(uriSkynetPrefix)) {
    return str.slice(uriSkynetPrefix.length);
  }
  return str;
}

module.exports = {
  defaultOptions,
  defaultPortalUrl,
  defaultSkynetPortalUrl,
  makeUrl,
  uriSkynetPrefix,
  walkDirectory,
  trimSiaPrefix,
};
