Skip to content

Commit

Permalink
chore: rename method
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 19, 2024
1 parent a53a13e commit 66177c4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ControllerType } from './index.js'
import type { NodeType } from './index.js'

export interface ConfigInit {
type?: ControllerType
type?: NodeType
}

export default (init: ConfigInit): any => {
Expand Down
4 changes: 2 additions & 2 deletions src/endpoint/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import boom from '@hapi/boom'
import { logger } from '@libp2p/logger'
import Joi from 'joi'
import { nanoid } from 'nanoid'
import type { Controller, Factory } from '../index.js'
import type { Node, Factory } from '../index.js'
import type { Server } from '@hapi/hapi'

const debug = logger('ipfsd-ctl:routes')
Expand All @@ -26,7 +26,7 @@ const badRequest = (err: Error & { stdout?: string }): void => {
throw boom.badRequest(msg)
}

const nodes: Record<string, Controller> = {}
const nodes: Record<string, Node> = {}

export default (server: Server, createFactory: () => Factory | Promise<Factory>): void => {
/**
Expand Down
22 changes: 11 additions & 11 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mergeOptions from 'merge-options'
import { isNode, isElectronMain } from 'wherearewe'
import KuboClient from './kubo/client.js'
import KuboDaemon from './kubo/daemon.js'
import type { Controller, ControllerOptions, ControllerOptionsOverrides, ControllerType, Factory, KuboController, KuboOptions, SpawnOptions } from './index.js'
import type { Node, NodeOptions, NodeOptionsOverrides, NodeType, Factory, KuboNode, KuboOptions, SpawnOptions } from './index.js'

const merge = mergeOptions.bind({ ignoreUndefined: true })

Expand All @@ -17,9 +17,9 @@ const defaults = {
forceKillTimeout: 5000
}

export interface FactoryInit extends ControllerOptions {
export interface FactoryInit extends NodeOptions {
/**
* Endpoint URL to manage remote Controllers. (Defaults: 'http://127.0.0.1:43134')
* Endpoint URL to manage remote Nodes. (Defaults: 'http://127.0.0.1:43134')
*/
endpoint?: string
}
Expand All @@ -28,13 +28,13 @@ export interface FactoryInit extends ControllerOptions {
* Factory class to spawn ipfsd controllers
*/
class DefaultFactory implements Factory<any> {
public options: ControllerOptions
public controllers: Controller[]
public readonly overrides: ControllerOptionsOverrides
public options: NodeOptions
public controllers: Node[]
public readonly overrides: NodeOptionsOverrides

private readonly endpoint: string

constructor (options: FactoryInit = {}, overrides: ControllerOptionsOverrides = {}) {
constructor (options: FactoryInit = {}, overrides: NodeOptionsOverrides = {}) {
this.endpoint = options.endpoint ?? process.env.IPFSD_CTL_SERVER ?? 'http://localhost:43134'
this.options = merge(defaults, options)
this.overrides = merge({
Expand All @@ -45,11 +45,11 @@ class DefaultFactory implements Factory<any> {
}

/**
* Spawn an IPFSd Controller
* Spawn an IPFSd Node
*/
async spawn (options?: KuboOptions & SpawnOptions): Promise<KuboController>
async spawn (options?: ControllerOptions & SpawnOptions): Promise<Controller> {
const type: ControllerType = options?.type ?? this.options.type ?? 'kubo'
async spawn (options?: KuboOptions & SpawnOptions): Promise<KuboNode>
async spawn (options?: NodeOptions & SpawnOptions): Promise<Node> {
const type: NodeType = options?.type ?? this.options.type ?? 'kubo'
const opts = merge({}, this.options, this.overrides[type], options)
let ctl: any

Expand Down
36 changes: 18 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Server from './endpoint/server.js'
import DefaultFactory from './factory.js'
import type { KuboController, KuboOptions } from './kubo/index.js'
import type { KuboNode, KuboOptions } from './kubo/index.js'

export * from './kubo/index.js'
export type ControllerType = 'kubo'
export type NodeType = 'kubo'

export interface Controller<API = unknown, Options = ControllerOptions, Info extends Record<string, any> = Record<string, any>, InitArgs = unknown, StartArgs = unknown, StopArgs = unknown, CleanupArgs = unknown> {
export interface Node<API = unknown, Options = NodeOptions, Info extends Record<string, any> = Record<string, any>, InitArgs = unknown, StartArgs = unknown, StopArgs = unknown, CleanupArgs = unknown> {
api: API
options: Options

Expand Down Expand Up @@ -66,11 +66,11 @@ export interface CircuitRelayOptions {
hop: CircuitRelayHopOptions
}

export interface ControllerOptions<InitOptions = unknown, StartOptions = unknown> {
export interface NodeOptions<InitOptions = unknown, StartOptions = unknown> {
/**
* The type of controller
*/
type?: ControllerType
type?: NodeType

/**
* Flag to activate custom config for tests
Expand Down Expand Up @@ -112,7 +112,7 @@ export interface ControllerOptions<InitOptions = unknown, StartOptions = unknown
start?: StartOptions
}

export interface ControllerOptionsOverrides {
export interface NodeOptionsOverrides {
kubo?: KuboOptions
}

Expand All @@ -123,12 +123,12 @@ export interface SpawnOptions {
remote?: true
}

export interface Factory<DefaultController extends Controller = Controller> {
export interface Factory<DefaultNode extends Node = Node> {
/**
* Create a node
*/
spawn(options?: KuboOptions & SpawnOptions): Promise<KuboController>
spawn(options?: ControllerOptions & SpawnOptions): Promise<DefaultController>
spawn(options?: KuboOptions & SpawnOptions): Promise<KuboNode>
spawn(options?: NodeOptions & SpawnOptions): Promise<DefaultNode>

/**
* Shut down all previously created nodes that are still running
Expand All @@ -138,41 +138,41 @@ export interface Factory<DefaultController extends Controller = Controller> {
/**
* The previously created nodes that are still running
*/
controllers: Controller[]
controllers: Node[]

/**
* The default options that will be applied to all nodes
*/
options: ControllerOptions
options: NodeOptions

/**
* Config overrides that will be applied to specific node types
*/
overrides: ControllerOptionsOverrides
overrides: NodeOptionsOverrides
}

/**
* Creates a factory
*/
export function createFactory (options: KuboOptions, overrides?: ControllerOptionsOverrides): Factory<KuboController>
export function createFactory (options?: ControllerOptions, overrides?: ControllerOptionsOverrides): Factory<Controller>
export function createFactory (options?: ControllerOptions, overrides?: ControllerOptionsOverrides): Factory<Controller> {
export function createFactory (options: KuboOptions, overrides?: NodeOptionsOverrides): Factory<KuboNode>
export function createFactory (options?: NodeOptions, overrides?: NodeOptionsOverrides): Factory<Node>
export function createFactory (options?: NodeOptions, overrides?: NodeOptionsOverrides): Factory<Node> {
return new DefaultFactory(options, overrides)
}

/**
* Creates a node
*/
export async function createController (options: KuboOptions & SpawnOptions): Promise<KuboController>
export async function createController (options?: any): Promise<any> {
export async function createNode (options: KuboOptions & SpawnOptions): Promise<KuboNode>
export async function createNode (options?: any): Promise<any> {
const f = new DefaultFactory()
return f.spawn(options)
}

/**
* Create a Endpoint Server
*/
export const createServer = (options?: number | { port: number }, factoryOptions: ControllerOptions = {}, factoryOverrides: ControllerOptionsOverrides = {}): Server => {
export const createServer = (options?: number | { port: number }, factoryOptions: NodeOptions = {}, factoryOverrides: NodeOptionsOverrides = {}): Server => {
let port: number | undefined

if (typeof options === 'number') {
Expand Down
6 changes: 3 additions & 3 deletions src/kubo/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KuboController, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions } from './index.js'
import type { KuboNode, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions } from './index.js'
import type { PeerInfo } from '@libp2p/interface'
import type { KuboRPCClient } from 'kubo-rpc-client'

Expand All @@ -10,9 +10,9 @@ export interface KuboClientInit extends KuboOptions {
}

/**
* Controller for remote nodes
* Node for remote nodes
*/
export default class KuboClient implements KuboController {
export default class KuboClient implements KuboNode {
public options: KuboOptions & Required<Pick<KuboOptions, 'rpc'>>
public peerInfo?: PeerInfo
public id: string
Expand Down
6 changes: 3 additions & 3 deletions src/kubo/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mergeOptions from 'merge-options'
import pDefer from 'p-defer'
import waitFor from 'p-wait-for'
import { checkForRunningApi, tmpDir, buildStartArgs, repoExists, buildInitArgs } from './utils.js'
import type { KuboController, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions } from './index.js'
import type { KuboNode, KuboInfo, KuboInitOptions, KuboOptions, KuboStartOptions } from './index.js'
import type { Logger } from '@libp2p/interface'
import type { KuboRPCClient } from 'kubo-rpc-client'

Expand All @@ -19,9 +19,9 @@ function translateError (err: Error & { stdout: string, stderr: string }): Error
}

/**
* Controller for daemon nodes
* Node for daemon nodes
*/
export default class KuboDaemon implements KuboController {
export default class KuboDaemon implements KuboNode {
public options: KuboOptions & Required<Pick<KuboOptions, 'rpc'>>

private readonly disposable: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/kubo/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Controller, ControllerOptions } from '../index.js'
import type { Node, NodeOptions } from '../index.js'
import type { KuboRPCClient } from 'kubo-rpc-client'

export interface KuboInit {
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface KuboStartOptions {
args?: string[]
}

export interface KuboOptions extends ControllerOptions<boolean | KuboInitOptions, boolean | KuboStartOptions> {
export interface KuboOptions extends NodeOptions<boolean | KuboInitOptions, boolean | KuboStartOptions> {
type: 'kubo'

/**
Expand Down Expand Up @@ -67,6 +67,6 @@ export interface KuboInfo {
repo: string
}

export interface KuboController extends Controller<KuboRPCClient, KuboOptions, KuboInfo, KuboInitOptions, KuboStartOptions> {
export interface KuboNode extends Node<KuboRPCClient, KuboOptions, KuboInfo, KuboInitOptions, KuboStartOptions> {

}
6 changes: 3 additions & 3 deletions test/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import merge from 'merge-options'
import { isBrowser, isWebWorker, isNode, isElectronMain } from 'wherearewe'
import { createFactory } from '../src/index.js'
import { repoExists } from '../src/kubo/utils.js'
import type { Factory, KuboOptions, SpawnOptions, KuboController } from '../src/index.js'
import type { Factory, KuboOptions, SpawnOptions, KuboNode } from '../src/index.js'

const types: Array<KuboOptions & SpawnOptions> = [{
type: 'kubo'
Expand All @@ -18,10 +18,10 @@ const types: Array<KuboOptions & SpawnOptions> = [{
remote: true
}]

describe('Controller API', function () {
describe('Node API', function () {
this.timeout(60000)

let factory: Factory<KuboController>
let factory: Factory<KuboNode>

before(async () => {
factory = createFactory({
Expand Down
22 changes: 11 additions & 11 deletions test/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { expect } from 'aegir/chai'
import * as kubo from 'kubo'
import { create as createKuboRPCClient } from 'kubo-rpc-client'
import { isNode, isElectronMain } from 'wherearewe'
import { createFactory, createController, createServer, type KuboOptions, type SpawnOptions, type KuboController, type Factory } from '../src/index.js'
import { createFactory, createNode, createServer, type KuboOptions, type SpawnOptions, type KuboNode, type Factory } from '../src/index.js'
import KuboClient from '../src/kubo/client.js'
import KuboDaemon from '../src/kubo/daemon.js'
import type Server from '../src/endpoint/server.js'

describe('`createController` should return the correct class', () => {
let node: KuboController
describe('`createNode` should return the correct class', () => {
let node: KuboNode

afterEach(async () => {
await node?.stop()
})

it('for type `kubo` ', async () => {
node = await createController({
node = await createNode({
type: 'kubo',
test: true,
disposable: false,
Expand All @@ -35,7 +35,7 @@ describe('`createController` should return the correct class', () => {
})

it('for remote', async () => {
node = await createController({
node = await createNode({
type: 'kubo',
test: true,
remote: true,
Expand All @@ -60,32 +60,32 @@ const types: Array<KuboOptions & SpawnOptions> = [{
bin: isNode ? kubo.path() : undefined
}]

describe('`createController({test: true})` should return daemon with test profile', () => {
let node: KuboController
describe('`createNode({test: true})` should return daemon with test profile', () => {
let node: KuboNode

afterEach(async () => {
await node?.stop()
})

for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
node = await createController(opts)
node = await createNode(opts)
expect(await node.api.config.get('Bootstrap')).to.be.empty()
await node.stop()
})
}
})

describe('`createController({test: true})` should return daemon with correct config', () => {
let node: KuboController
describe('`createNode({test: true})` should return daemon with correct config', () => {
let node: KuboNode

afterEach(async () => {
await node?.stop()
})

for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
node = await createController(opts)
node = await createNode(opts)
const swarm = await node.api.config.get('Addresses.Swarm')

expect(swarm).to.include('/ip4/127.0.0.1/tcp/0')
Expand Down
6 changes: 3 additions & 3 deletions test/kubo/utils.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path'
import { expect } from 'aegir/chai'
import * as kubo from 'kubo'
import { create as createKuboRPCClient } from 'kubo-rpc-client'
import { createFactory, createController } from '../../src/index.js'
import { createFactory, createNode } from '../../src/index.js'
import { tmpDir, checkForRunningApi, repoExists, removeRepo, buildStartArgs, buildInitArgs } from '../../src/kubo/utils.js'

describe('utils', function () {
Expand All @@ -24,7 +24,7 @@ describe('utils', function () {
})

it('should return path to api with running node', async () => {
const node = await createController({
const node = await createNode({
test: true,
type: 'kubo',
rpc: createKuboRPCClient,
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('utils', function () {

describe('repoExists', () => {
it('should resolve true when repo exists', async () => {
const node = await createController({
const node = await createNode({
type: 'kubo',
test: true,
rpc: createKuboRPCClient,
Expand Down

0 comments on commit 66177c4

Please sign in to comment.