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

Add alias option #56

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions bin/hyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -46,6 +47,7 @@ const commands = {
unseed,
create,
beam,
alias,

driveLs,
driveCat,
Expand Down
42 changes: 42 additions & 0 deletions lib/commands/alias.js
Original file line number Diff line number Diff line change
@@ -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)
})

}
}
2 changes: 1 addition & 1 deletion lib/commands/beam.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions lib/urls.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:`)}
Expand Down