Skip to content

Commit

Permalink
Changed name to store
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Apr 2, 2019
1 parent af6a023 commit cea876b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 137 deletions.
67 changes: 35 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
# dat-pin
An extension for the Dat CLI to support Pinning Services
# dat-store
An extension for the Dat CLI to support storage providers

```shell
npm install -g dat-pin
npm install -g dat-store

# Run a pinning service on http://localhost:3282
# Run a store on http://localhost:3282
dat-pin run-service

# Add a dat to the pinning service so it's always being shared
dat-pin add dat://0a9e202b8055721bd2bc93b3c9bbc03efdbda9cfee91f01a123fdeaadeba303e/
# Add a dat to the so it's always being shared
dat-store dat://0a9e202b8055721bd2bc93b3c9bbc03efdbda9cfee91f01a123fdeaadeba303e/

# Install local pinning service
dat-pin install-service
# Install local
dat-store install-service

# Configure external pinning service
dat-pin set-service https://hashbase.io/
dat-pin login -u yourusername -p yourpassword
# Configure external storage provider
dat-store set-provider https://hashbase.io/
dat-store login yourusername
```

## Commands

```
dat-store <url>
Add a Dat to your storage provider.
Commands:
dat-pin add Pin a `dat://` read key to your pinning service to
keep it online
dat-pin remove Remove a `dat://` read key from your pinning
service
dat-pin list List the `dat://` read keys that you've pinned
dat-pin set-service Set the URL of the pinning service you want to use
dat-pin unset-service Resets your preferences to use your local pinning
service
dat-pin get-service Get the URL for your pinning service
dat-pin login Logs you into the configured pinning service. Not
necessary for local services
dat-pin logout Logs you out of the pinning service
dat-pin run-service Runs the pinning service without installing it in
the background.
dat-pin install-service Installs a local pinning service on your computer.
This will run in the background while your computer
is active.
dat-pin uninstall-service Uninstalls your local pinning service.
dat-store add <url> Add a Dat to your storage provider.
[default]
dat-store remove <url> Remove a Dat from your storage
provider.
dat-store list List the Dats in your storage provider.
dat-store set-provider <url> Set the URL of your storage provider.
dat-store get-provider Get the URL of your storage provider.
dat-store unset-provider Reset your storage provider to the
default: http://localhost:3472
dat-store login <username> [password] Logs you into your storage provider.
dat-store logout Logs you out of your storage provider.
dat-store run-service Runs a local storage provider.
dat-store install-service Installs a storage service on your
machine. This will run in the
background while your computer is
active.
dat-store uninstall-service Uninstalls your local storage service.
```

## How it works:

- Uses [dat-pinning-service-client](https://github.com/beakerbrowser/dat-pinning-service-client) to talk to pinning services that adhere to [DEP 0003](https://www.datprotocol.com/deps/0003-http-pinning-service-api/)
- Can start a local pinning service called `dat-pin` using `dat-pin install-service` (uses [os-service](https://www.npmjs.com/package/os-service))
- Runs on `127.0.0.1:3472`
- Uses [dat-pinning-service-client](https://github.com/beakerbrowser/dat-pinning-service-client) to talk to storage providers that adhere to [DEP 0003](https://www.datprotocol.com/deps/0003-http-pinning-service-api/)
- Can start a local called `dat-store` using `dat-store install-service` (uses [os-service](https://www.npmjs.com/package/os-service))
- Runs on `http://localhost:3472`
- Binds to port `3282` for interacting with the P2P network
- The service uses [dat-librarian](https://www.npmjs.com/package/dat-librarian) to manage archives
- The service acts as a discovery gateway for [discovery-swarm-stream](https://www.npmjs.com/package/discovery-swarm-stream)
10 changes: 5 additions & 5 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ const fs = require('fs-extra')
const { DatPinningServiceClient } = require('dat-pinning-service-client')
const userhome = require('userhome')

const debug = require('debug')('dat-pin:client')
const debug = require('debug')('dat-store:client')

// Default location for pinning service config
const CONFIG_LOCATION = userhome('.dat', 'pinning.json')
// Default location for config
const CONFIG_LOCATION = userhome('.dat', 'store.json')

// URL for pinning daemon running on localhost
// URL for storage provider running on localhost
const LOCAL_SERVICE = 'http://localhost:3472'

module.exports =

class PinClient {
class StoreClient {
constructor ({ configLocation }) {
this.configLocation = configLocation || CONFIG_LOCATION
}
Expand Down
126 changes: 48 additions & 78 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,30 @@
let yargs = require('yargs')

const SERVICE_NAME = 'dat-pin'

const commands = [{
name: 'add',
command: add,
help: 'Pin a `dat://` read key to your pinning service to keep it online'
}, {
name: 'remove',
command: remove,
help: 'Remove a `dat://` read key from your pinning service'
}, {
name: 'list',
command: list,
help: 'List the `dat://` read keys that you\'ve pinned'
}, {
name: 'set-service',
command: setService,
help: 'Set the URL of the pinning service you want to use'
}, {
name: 'unset-service',
command: unsetService,
help: 'Resets your preferences to use your local pinning service'
}, {
name: 'get-service',
command: getService,
help: 'Get the URL for your pinning service'
}, {
name: 'login',
command: login,
options: [{
name: 'username',
abbr: 'u'
}, {
name: 'password',
abbr: 'p'
}],
help: 'Logs you into the configured pinning service. Not necessary for local services'
}, {
name: 'logout',
command: logout,
help: 'Logs you out of the pinning service'
}, {
name: 'run-service',
command: runService,
help: 'Runs the pinning service without installing it in the background.'
}, {
name: 'install-service',
command: installService,
help: 'Installs a local pinning service on your computer. This will run in the background while your computer is active.'
}, {
name: 'uninstall-service',
command: uninstallService,
help: 'Uninstalls your local pinning service.'
}]

for(let {name, command, help, options} of commands) {
yargs = yargs.command(name, help, (yargs) => {
if(!options) return yargs
return options.reduce((yargs, {name, abbr, help}) => {
return yargs.option(name, {
alias: abbr,
describe: help,
demandOption: true
})
}, yargs)
}, command)
}

yargs = yargs.scriptName('dat-pin').help()
const yargs = require('yargs')

const SERVICE_NAME = 'dat-store'

const commands = yargs
.scriptName(SERVICE_NAME)
.command(['add <url>', '$0 <url>'], 'Add a Dat to your storage provider.', () => void 0, add)
.command('remove <url>', 'Remove a Dat from your storage provider.', () => void 0, remove)
.command('list', 'List the Dats in your storage provider.', () => void 0, list)
.command('set-provider <url>', 'Set the URL of your storage provider.', () => void 0, setService)
.command('get-provider', 'Get the URL of your storage provider.', () => void 0, getService)
.command('unset-provider', 'Reset your storage provider to the default: http://localhost:3472', () => void 0, unsetService)
.command('login <username> [password]', 'Logs you into your storage provider.', () => void 0, login)
.command('logout', 'Logs you out of your storage provider.', () => void 0, logout)
.command('run-service', 'Runs a local storage provider.', (yargs) => {
yargs
.option('storage-location')
}, runService)
.command('install-service', 'Installs a storage service on your machine. This will run in the background while your computer is active.', (yargs) => {
yargs
.option('storage-location')
}, installService)
.command('uninstall-service', 'Uninstalls your local storage service.', () => void 0, uninstallService)
.help()

module.exports = (argv) => {
yargs.parse(argv)
commands.parse(argv)
}

function getClient (args) {
Expand All @@ -83,7 +36,7 @@ function getClient (args) {
}

async function add (args) {
await getClient(args).add(args._[1])
await getClient(args).add(args.url)
}

async function list (args) {
Expand All @@ -95,11 +48,11 @@ async function list (args) {
}

async function remove (args) {
await getClient(args).remove(args._[1])
await getClient(args).remove(args.url)
}

async function setService (args) {
await getClient(args).setService(args._[1])
await getClient(args).setService(args.url)
}

async function unsetService (args) {
Expand All @@ -113,7 +66,18 @@ async function getService (args) {
}

async function login (args) {
await getClient(args).login(args.username, args.password)
const { username, password } = args

const client = await getClient(args)

if (!password) {
const read = require('read')

read({ prompt: 'Enter your password:', silent: true }, (err, password) => {
if (err) throw err
client.login(username, password)
})
} else client.login(username, password)
}

async function logout (args) {
Expand All @@ -125,15 +89,21 @@ function getServiceLocation () {
return path.join(__dirname, 'service.js')
}

function runService() {
function runService () {
require('./service.js')
}

async function installService (args) {
const service = require('os-service')
const programPath = getServiceLocation()

service.add(SERVICE_NAME, { programPath }, (e) => {
const programArgs = []

if (args.storageLocation) {
programArgs.push('--storage-location', args.storageLocation)
}

service.add(SERVICE_NAME, { programPath, programArgs }, (e) => {
if (e) throw e
})
}
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dat-pin",
"name": "dat-store",
"version": "1.0.1",
"description": "An extension for the Dat CLI to support Pinning Services",
"description": "An extension for the Dat CLI to support s",
"main": "index.js",
"scripts": {
"test": "node test"
Expand Down Expand Up @@ -35,12 +35,11 @@
"fastify-websocket": "^0.3.0",
"fs-extra": "^7.0.1",
"os-service": "^2.1.3",
"read": "^1.0.7",
"userhome": "^1.0.0",
"yargs": "^13.2.2"
},
"devDependencies": {
"delay": "^4.1.0",
"tape": "^4.10.1",
"wtfnode": "^0.8.0"
"tape": "^4.10.1"
}
}
16 changes: 8 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const DAT_SWARM_DEFAULTS = require('dat-swarm-defaults')
const SWARM_OPTS = DAT_SWARM_DEFAULTS({
hash: false
})
const DEFAULT_STORAGE_LOCATION = userhome('.dat', 'pinning-data')
const DEFAULT_STORAGE_LOCATION = userhome('.dat', 'store-data')
const DEFAULT_PORT = 3472
const DEFAULT_HOST = '127.0.0.1'
const DEFAULT_HOST = '::'

module.exports =

class PinServer {
class StoreServer {
static async createServer (options) {
const server = new PinServer()
const server = new StoreServer()

await server.init(options)

Expand Down Expand Up @@ -50,7 +50,7 @@ class PinServer {
this.fastify.get('/.well-known/psa', async () => {
return {
'PSA': 1,
'title': 'My Pinning Service',
'title': 'My ',
'description': 'Keep your Dats online!',
'links': [{
'rel': 'https://archive.org/services/purl/purl/datprotocol/spec/pinning-service-account-api',
Expand Down Expand Up @@ -127,7 +127,7 @@ class PinServer {
}

async getMetadata (key) {
const {archive} = await this.librarian.get(key)
const { archive } = await this.librarian.get(key)

let manifest = {
name: key
Expand All @@ -150,13 +150,13 @@ class PinServer {
async destroy () {
await new Promise((resolve, reject) => {
this.fastify.close((err) => {
if(err) reject(err)
if (err) reject(err)
else resolve()
})
})
await new Promise((resolve, reject) => {
this.dss.destroy((err) => {
if(err) reject(err)
if (err) reject(err)
else resolve()
})
})
Expand Down
9 changes: 7 additions & 2 deletions service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const PinServer = require('./server')

// TODO: Parse a config file somewhere
const yargs = require('yargs')

const argv = yargs
.command('$0', 'Start the store service')
.string('storage-location')
.argv

const service = require('os-service')

Expand All @@ -14,5 +19,5 @@ run().catch((e) => {
})

async function run () {
await PinServer.createServer({})
await PinServer.createServer(argv)
}
Loading

0 comments on commit cea876b

Please sign in to comment.