Skip to content

Commit

Permalink
fix: use configured endpoint instead of default (#824)
Browse files Browse the repository at this point in the history
Allow passing endpoint to client
  • Loading branch information
achingbrain committed Apr 19, 2024
1 parent 4e769a0 commit e0d2f88
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
18 changes: 4 additions & 14 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Node, NodeOptions, NodeOptionsOverrides, NodeType, Factory, KuboNo
const merge = mergeOptions.bind({ ignoreUndefined: true })

const defaults = {
endpoint: process.env.IPFSD_CTL_SERVER ?? 'http://localhost:43134',
remote: !isNode && !isElectronMain,
disposable: true,
test: false,
Expand All @@ -17,13 +18,6 @@ const defaults = {
forceKillTimeout: 5000
}

export interface FactoryInit extends NodeOptions {
/**
* Endpoint URL to manage remote Nodes. (Defaults: 'http://127.0.0.1:43134')
*/
endpoint?: string
}

/**
* Factory class to spawn ipfsd controllers
*/
Expand All @@ -32,11 +26,8 @@ class DefaultFactory implements Factory<any> {
public controllers: Node[]
public readonly overrides: NodeOptionsOverrides

private readonly endpoint: string

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

if (type === 'kubo') {
if (opts.remote === true) {
const req = await fetch(`${this.endpoint}/spawn`, {
const req = await fetch(`${opts.endpoint}/spawn`, {
method: 'POST',
headers: {
'content-type': 'application/json'
Expand All @@ -68,7 +59,6 @@ class DefaultFactory implements Factory<any> {
const result = await req.json()

ctl = new KuboClient({
endpoint: this.endpoint,
...opts,
...result
})
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ export interface NodeOptions<InitOptions = unknown, StartOptions = unknown> {
*/
disposable?: boolean

/**
* Where applicable, this endpoint will be used to spawn nodes remotely
*
* @default http://127.0.0.1:43134
*/
endpoint?: string

/**
* Additional environment variables, passed to executing shell. Only applies
* for Daemon controllers
Expand Down Expand Up @@ -294,7 +301,7 @@ export async function createNode (options?: any): Promise<any> {
/**
* Create a Endpoint Server
*/
export const createServer = (options?: number | { port: number }, factoryOptions: NodeOptions = {}, factoryOverrides: NodeOptionsOverrides = {}): Server => {
export const createServer = (options?: number | { port: number }, factoryOptions: KuboOptions | NodeOptions = {}, factoryOverrides: NodeOptionsOverrides = {}): Server => {
let port: number | undefined

if (typeof options === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion src/kubo/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class KuboDaemon implements KuboNode {

constructor (options: KuboOptions) {
if (options.rpc == null) {
throw new Error('Please pass an rcp option')
throw new Error('Please pass an rpc option')
}

// @ts-expect-error cannot detect rpc is present
Expand Down
28 changes: 28 additions & 0 deletions test/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,40 @@ describe('`createServer`', () => {
server = createServer()
expect(server.port).to.be.eq(43134)
})

it('should return a Server with port 11111 when passed number directly', () => {
server = createServer(11111)
expect(server.port).to.be.eq(11111)
})

it('should return a Server with port 22222 when passed {port: 22222}', () => {
server = createServer({ port: 22222 })
expect(server.port).to.be.eq(22222)
})

it('should use server from factory', async () => {
if (!isNode && !isElectronMain) {
return
}

server = createServer({ port: 22222 }, {
type: 'kubo',
test: true,
disposable: false,
rpc: createKuboRPCClient,
bin: isNode ? kubo.path() : undefined
})
await server.start()

const factory = createFactory({
endpoint: `http://127.0.0.1:${server.port}`
})

const node = await factory.spawn({
type: 'kubo',
remote: true,
rpc: createKuboRPCClient
})
await node.stop()
})
})

0 comments on commit e0d2f88

Please sign in to comment.