Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

WIP: Make chunking config same as web3 #116

Open
wants to merge 3 commits into
base: main
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
50 changes: 29 additions & 21 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@ const serialize = ({ ...data }) =>
*/
const deserialize = (text) => CBOR.codec.decode(Buffer.from(text, 'binary'))

// @ts-ignore
export const settings = new Conf({
projectName: cliSettings.projectName,
fileExtension: 'cbor',
serialize,
deserialize,
})

const client = new W3Client({
//@ts-ignore
serviceDID: cliSettings.W3_STORE_DID,
serviceURL: cliSettings.SERVICE_URL,
//@ts-ignore
accessDID: cliSettings.ACCESS_DID,
accessURL: cliSettings.ACCESS_URL,
settings,
})

export default client
// TODO this will go away later?
function mergeSettings(profileSettings) {
if (profileSettings.size) {
return
}

export function getClient(profile = 'main') {
const settings = new Conf({
const oldSettings = new Conf({
projectName: cliSettings.projectName,
fileExtension: 'cbor',
serialize,
deserialize,
})

if (oldSettings.size) {
profileSettings.store = { ...oldSettings.store }
}
}

export function getProfileSettings(profile = 'main') {
// @ts-ignore
const profileSettings = new Conf({
projectName: 'w3up',
projectSuffix: '',
configName: profile,
Expand All @@ -44,6 +43,15 @@ export function getClient(profile = 'main') {
deserialize,
})

// TODO: remove this when no longer needed.
mergeSettings(profileSettings)

return profileSettings
}

export function getClient(profile = 'main') {
const settings = getProfileSettings(profile)

const client = new W3Client({
//@ts-ignore
serviceDID: cliSettings.W3_STORE_DID,
Expand Down
3 changes: 2 additions & 1 deletion src/commands/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import ora from 'ora'

import { getClient } from '../client.js'
import { hasSetupAccount } from '../validation.js'

/**
* @typedef {import('yargs').Arguments<{did?:string, profile: string}>} DelegateArgs
Expand All @@ -26,7 +27,7 @@ const exe = async ({ did, profile }) => {
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
const build = (yargs) => yargs
const build = (yargs) => yargs.check(hasSetupAccount)

const id = {
command: 'delegate <did>',
Expand Down
4 changes: 2 additions & 2 deletions src/commands/id.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ora from 'ora'

import { getClient, settings } from '../client.js'
import { getClient, getProfileSettings } from '../client.js'

/**
* @typedef {{reset?:boolean, profile: string}} Id
Expand All @@ -16,7 +16,7 @@ const exe = async (args) => {
const view = ora({ spinner: 'line' })

if (args.reset) {
settings.clear()
getProfileSettings(args.profile)?.clear()
}

const client = getClient(args.profile)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/importDelegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import ora from 'ora'

import { getClient } from '../client.js'
import { isPath } from '../validation.js'
import { hasID, isPath } from '../validation.js'

/**
* @typedef {{fileName?:string, alias?:string, profile: string}} ImportDelegation
Expand Down Expand Up @@ -36,7 +36,7 @@ const exe = async ({ fileName, alias = '', profile }) => {
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
const builder = (yargs) => yargs.check(checkFileName)
const builder = (yargs) => yargs.check(hasID).check(checkFileName)

/**
*
Expand Down
3 changes: 0 additions & 3 deletions src/commands/info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import ora, { oraPromise } from 'ora'

import { settings } from '../client.js'
import { default as info } from '../settings.js'

/**
Expand Down
24 changes: 11 additions & 13 deletions src/commands/insights.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as API from '@ucanto/interface'
import { parseLink } from '@ucanto/server'
import ora from 'ora'

import client from '../client.js'
import { hasID, isCID } from '../validation.js'
import { getClient } from '../client.js'
import { hasSetupAccount, isCID } from '../validation.js'

/**
* @typedef {{cid?:API.Link, ws?:boolean, subscribe?:boolean, insight_data?: any}} Insights
* @typedef {{cid?:API.Link, ws?:boolean, subscribe?:boolean, insight_data?: any, profile?:string}} Insights
* @typedef {import('yargs').Arguments<Insights>} InsightsArgs
*/

Expand All @@ -16,13 +16,14 @@ import { hasID, isCID } from '../validation.js'
* @param {InsightsArgs} argv
* @returns {Promise<void>}
*/
const exe = async ({ cid, ws, subscribe }) => {
const exe = async ({ cid, ws, subscribe, profile }) => {
const spinner = ora({ text: `Getting insights for ${cid}`, spinner: 'line' })
const shouldWS = ws || subscribe

if (shouldWS) {
spinner.fail(`⚠️Subscriptions not yet supported ⚠️`)
} else {
const client = getClient(profile)
/***
* @type Insights
*/
Expand All @@ -34,15 +35,12 @@ const exe = async ({ cid, ws, subscribe }) => {
* @type {import('yargs').CommandBuilder} yargs
*/
const builder = (yargs) =>
yargs
.check(() => hasID())
.check(checkCID)
.option('subscribe', {
type: 'boolean',
alias: 'ws',
showInHelp: true,
describe: 'Get a Subscription to incoming insights',
})
yargs.check(hasSetupAccount).check(checkCID).option('subscribe', {
type: 'boolean',
alias: 'ws',
showInHelp: true,
describe: 'Get a Subscription to incoming insights',
})

/**
* @param {InsightsArgs} argv
Expand Down
16 changes: 7 additions & 9 deletions src/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ora, { oraPromise } from 'ora'

import { getClient } from '../client.js'
import { buildSimpleConsoleTable } from '../utils.js'
import { hasID } from '../validation.js'
import { hasID, hasSetupAccount } from '../validation.js'

/**
* @typedef {{verbose?:boolean, profile: string}} List
Expand Down Expand Up @@ -90,14 +90,12 @@ const exe = async (argv) => {

/** @type {import('yargs').CommandBuilder} yargs */
const builder = (yargs) =>
yargs
// .check(() => hasID())
.option('verbose', {
type: 'boolean',
alias: 'verbose',
showInHelp: true,
describe: 'Show more columns in the list, such as the Uploaded CAR CID',
})
yargs.check(hasSetupAccount).option('verbose', {
type: 'boolean',
alias: 'verbose',
showInHelp: true,
describe: 'Show more columns in the list, such as the Uploaded CAR CID',
})

export default {
command: 'list',
Expand Down
4 changes: 2 additions & 2 deletions src/commands/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const exe = async (argv) => {
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
// const builder = (yargs) => yargs.check(() => hasID()).check(checkEmail)
const builder = (yargs) => yargs.check(hasID).check(checkEmail)

/**
* @param {RegisterArgs} argv
Expand All @@ -58,6 +58,6 @@ const checkEmail = (argv) => {
export default {
command: 'register <email>',
describe: 'Register your UCAN Identity with w3up',
// builder,
builder,
handler: exe,
}
12 changes: 6 additions & 6 deletions src/commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as API from '@ucanto/interface'
import { parseLink } from '@ucanto/server'
import ora from 'ora'

import client from '../client.js'
import { hasID, isCID } from '../validation.js'
import { getClient } from '../client.js'
import { hasID, hasSetupAccount, isCID } from '../validation.js'

/**
* @typedef {{cid?:API.Link}} Remove
* @typedef {{cid?:API.Link, profile?:string}} Remove
* @typedef {import('yargs').Arguments<Remove>} RemoveArgs
*/

Expand All @@ -16,17 +16,17 @@ import { hasID, isCID } from '../validation.js'
* @param {RemoveArgs} argv
* @returns {Promise<void>}
*/
const exe = async ({ cid }) => {
const exe = async ({ cid, profile }) => {
const view = ora(`Unlinking ${cid}...`).start()
const res = await client.remove(parseLink(cid))
const res = await getClient(profile).remove(parseLink(cid))
view.succeed(`${res.toString()}`)
}

/**
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
const builder = (yargs) => yargs.check(() => hasID()).check(checkCID)
const builder = (yargs) => yargs.check(hasSetupAccount).check(checkCID)

/**
* @param {RemoveArgs} argv
Expand Down
3 changes: 2 additions & 1 deletion src/commands/switchAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Delegation } from '@ucanto/server'
import inquirer from 'inquirer'

import { getClient } from '../client.js'
import { hasID } from '../validation.js'
import listAccounts from './listAccounts.js'

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ async function inquirerPick(choices, client) {
* @returns {import('yargs').Argv<{}>}
*/
const builder = (yargs) =>
yargs.option('did', {
yargs.check(hasID).option('did', {
type: 'string',
showInHelp: true,
describe: 'select account by did',
Expand Down
18 changes: 2 additions & 16 deletions src/commands/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import toIterator from 'stream-to-it'
import { getClient } from '../client.js'
import { buildCar } from '../lib/car/buildCar.js'
import { logToFile } from '../lib/logging.js'
import { MAX_CAR_SIZE } from '../settings.js'
import { bytesToCarCID } from '../utils.js'
import { hasID, isPath, resolvePath } from '../validation.js'
import { checkPath, hasID, hasSetupAccount } from '../validation.js'

/**
* @typedef {{path?: string;split?: boolean; profile: string}} Upload
Expand Down Expand Up @@ -109,7 +108,7 @@ const exe = async (argv) => {
*/
const builder = (yargs) =>
yargs
// .check(() => hasID())
.check(hasSetupAccount)
.check(checkPath)
.option('chunk-size', {
type: 'number',
Expand All @@ -122,19 +121,6 @@ const builder = (yargs) =>
'Split the data into multiple when cars when size limit is hit.',
})

/**
* @param {UploadArgs} argv
*/
const checkPath = ({ path }) => {
try {
return isPath(path)
} catch (err) {
throw new Error(
`${path} is probably not a valid path to a file or directory: \n${err}`
)
}
}

export default {
command: ['upload <path>', 'import <path>'],
describe: 'Upload any file or directory to your account',
Expand Down
27 changes: 10 additions & 17 deletions src/commands/uploadCars.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import path from 'path'
// @ts-ignore
import toIterator from 'stream-to-it'

import client from '../client.js'
import { getClient } from '../client.js'
import { getAllFiles, isDirectory } from '../lib/car/file.js'
import { logToFile } from '../lib/logging.js'
import { MAX_CAR_SIZE } from '../settings.js'
import { hasID, isCarFile, isPath, resolvePath } from '../validation.js'
import {
checkPath,
hasID,
hasSetupAccount,
isCarFile,
resolvePath,
} from '../validation.js'

//gotta start somewhere. 3 is fine.
const MAX_CONNECTION_POOL_SIZE = 3
Expand Down Expand Up @@ -43,7 +49,7 @@ export async function uploadExistingCar(filePath, view) {
}

/**
* @typedef {{path?:string}} Upload
* @typedef {{path?:string, profile?:string}} Upload
* @typedef {import('yargs').Arguments<Upload>} UploadArgs
*/

Expand Down Expand Up @@ -99,20 +105,7 @@ const exe = async (argv) => {
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
const builder = (yargs) => yargs.check(() => hasID()).check(checkPath)

/**
* @param {UploadArgs} argv
*/
const checkPath = ({ path }) => {
try {
return isPath(path)
} catch (err) {
throw new Error(
`${path} is probably not a valid path to a file or directory: \n${err}`
)
}
}
const builder = (yargs) => yargs.check(hasSetupAccount).check(checkPath)

export default {
command: ['upload-cars <path>'],
Expand Down
8 changes: 3 additions & 5 deletions src/commands/whoami.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import ora from 'ora'

import { getClient } from '../client.js'
import { logToFile } from '../lib/logging.js'
import { hasID } from '../validation.js'
import { hasSetupAccount } from '../validation.js'

const exe = async (/** @type {{ profile: string | undefined; }} */ args) => {
const view = ora({ text: 'Checking identity', spinner: 'line' })
try {
const client = getClient(args.profile)
hasID(client)
const { agent, account } = await client.identity()
const response = await client.whoami()

Expand All @@ -34,12 +32,12 @@ Access Account: ${response}`)
* @type {import('yargs').CommandBuilder} yargs
* @returns {import('yargs').Argv<{}>}
*/
const builder = (yargs) => yargs.check(() => hasID())
const builder = (yargs) => yargs.check(hasSetupAccount)

export default {
command: 'whoami',
describe: 'Show your current UCAN Identity',
// builder,
builder,
handler: exe,
exampleOut: `DID:12345...`,
exampleIn: '$0 whoami',
Expand Down
Loading