diff --git a/README.md b/README.md index 0febddc..fe1d7ee 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ General Commands: hyp seed {urls...} - Download and make hyper data available to the network. hyp unseed {urls...} - Stop making hyper data available to the network. hyp create {drive|bee} - Create a new hyperdrive or hyperbee. - + hyp alias [alias] [full_url] - Create an alias for the urls. hyp beam {pass_phrase} - Send a stream of data over the network. Hyperdrive Commands: diff --git a/bin/hyp.js b/bin/hyp.js index 043bdf4..78a55f6 100755 --- a/bin/hyp.js +++ b/bin/hyp.js @@ -15,6 +15,7 @@ import create from '../lib/commands/create.js' import seed from '../lib/commands/seed.js' import unseed from '../lib/commands/unseed.js' import beam from '../lib/commands/beam.js' +import alias from '../lib/commands/alias.js' import driveLs from '../lib/commands/drive/ls.js' import driveCat from '../lib/commands/drive/cat.js' @@ -46,6 +47,7 @@ const commands = { unseed, create, beam, + alias, driveLs, driveCat, diff --git a/lib/commands/alias.js b/lib/commands/alias.js new file mode 100644 index 0000000..927905e --- /dev/null +++ b/lib/commands/alias.js @@ -0,0 +1,42 @@ +import chalk from 'chalk' +import os from 'os' +import path from 'path' +import fs from 'fs' +const FULL_USAGE = ` The alias command allows you to create aliases for the long urls...` //I will think of something + + +export default { + name: 'alias', + description: 'Create an alias for the urls', + usage: { + simple: '[alias] [full_url]', + full: FULL_USAGE + }, + command: async function (args) { + let url = args[1] + let alias = args[0] + if (!url || !alias){ + console.error(chalk.bold("Correct usage: ")+"hyp alias [alias] [full_url]") + process.exit(0) + } + if (!String(url).startsWith("hyper://") || String(url).length != 72 ){ + console.error("Invalid url") + process.exit(0) + } + datadir = path.join(os.homedir(),".hyperdrive") + if (fs.existsSync(datadir)){ + + }else { + fs.mkdirSync(datadir) + } + if (fs.existsSync(path.join(datadir,String(url).replace("hyper://","")))){ + fs.rmSync(path.join(datadir,String(url).replace("hyper://",""))) + } + fs.writeFileSync(path.join(datadir,String(url).replace("hyper://","")),alias) + + process.once('SIGINT', () => { + process.exit(0) + }) + + } +} diff --git a/lib/commands/beam.js b/lib/commands/beam.js index c38efef..e63ec6a 100644 --- a/lib/commands/beam.js +++ b/lib/commands/beam.js @@ -10,7 +10,7 @@ const FULL_USAGE = ` On the sending device: - cat hello.txt | hyp beam "for bob roberts" + cat hello.txt | hyp beam "for bob roberts" On the receiving device: diff --git a/lib/commands/seed.js b/lib/commands/seed.js index ab330e1..4ec8790 100644 --- a/lib/commands/seed.js +++ b/lib/commands/seed.js @@ -24,7 +24,7 @@ export default { var keys = [] for (let url of args._) { let urlp = parseHyperUrl(url) - keys.push(urlp.hostname) + keys.push(urlp.hostname) } for (const key of keys) { diff --git a/lib/urls.js b/lib/urls.js index 4815a18..b7570a9 100644 --- a/lib/urls.js +++ b/lib/urls.js @@ -1,12 +1,21 @@ -import { parse } from 'url' +import { parse } from 'url' +import { join } from 'path' +import { readFileSync, existsSync } from 'fs' const SCHEME_REGEX = /[a-z]+:\/\//i // 1 2 3 4 const VERSION_REGEX = /^(hyper:\/\/)?([^/]+)(\+[^/]+)(.*)$/i export function parseHyperUrl (str, parseQS) { - // prepend the scheme if it's missing - if (!SCHEME_REGEX.test(str)) { + // prepend the scheme if it's missing and if the url has 64 characters (and therefore is not an alias) + if (!SCHEME_REGEX.test(str) && str.length === 64) { str = 'hyper://' + str + }else{ + str = join(os.homedir(),".hyperdrive",str) + if (existsSync(str)){ + str = readFileSync(str) + } else { + throw new Error("Invalid url or alias") + } } var parsed, version = null, match = VERSION_REGEX.exec(str) @@ -15,6 +24,7 @@ export function parseHyperUrl (str, parseQS) { parsed = parse((match[1] || '') + (match[2] || '') + (match[4] || ''), parseQS) version = match[3].slice(1) } else { + parsed = parse(str, parseQS) } parsed.href = str // overwrite href to include actual original diff --git a/lib/usage.js b/lib/usage.js index 1a8b581..0bd6abc 100644 --- a/lib/usage.js +++ b/lib/usage.js @@ -4,7 +4,7 @@ export default function usage (commands, err, cmd) { if (err) { console.error(chalk.red(`${err}\n`)) } else { - console.error('') + console.error('') } if (cmd) { @@ -22,7 +22,7 @@ ${chalk.bold(`General Commands:`)} ${simple(commands.seed)} ${simple(commands.unseed)} ${simple(commands.create)} - + ${simple(commands.alias)} ${simple(commands.beam)} ${chalk.bold(`Hyperdrive Commands:`)}