Skip to content

Commit

Permalink
fix: types again (#652)
Browse files Browse the repository at this point in the history
More comprehensive types available.  Also turn factory into an interface.
  • Loading branch information
achingbrain committed Jul 30, 2021
1 parent 48778fd commit afe87c2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -7,6 +7,7 @@
"main": "src/index.js",
"jsdelivr": "dist/index.min.js",
"unpkg": "dist/index.min.js",
"types": "dist/src/index.d.ts",
"scripts": {
"lint": "aegir ts -p check && aegir lint",
"docs": "aegir docs",
Expand Down Expand Up @@ -42,6 +43,7 @@
"@hapi/hapi": "^20.0.0",
"debug": "^4.1.1",
"execa": "^5.0.0",
"ipfs-core-types": "^0.6.1",
"ipfs-utils": "^8.1.4",
"joi": "^17.2.1",
"merge-options": "^3.0.1",
Expand All @@ -58,6 +60,7 @@
"ipfs-client": "^0.5.1",
"ipfs-http-client": "^51.0.1",
"ipfs-unixfs": "^5.0.0",
"it-last": "^1.0.5",
"util": "^0.12.4"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/factory.js
Expand Up @@ -106,7 +106,7 @@ class Factory {
* Spawn an IPFSd Controller
*
* @param {ControllerOptions} options
* @returns {Promise<ControllerDaemon | ControllerProc | ControllerRemote>}
* @returns {Promise<Controller>}
*/
async spawn (options = { }) {
const type = options.type || this.opts.type || 'go'
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
@@ -1,12 +1,13 @@
'use strict'

const Factory = require('./factory')
const DefaultFactory = require('./factory')
const Server = require('./endpoint/server')

/**
* @typedef {import("./types").Controller} Controller
* @typedef {import("./types").ControllerOptions} ControllerOptions
* @typedef {import("./types").ControllerOptionsOverrides} ControllerOptionsOverrides
* @typedef {import('./types').Controller} Controller
* @typedef {import('./types').ControllerOptions} ControllerOptions
* @typedef {import('./types').ControllerOptionsOverrides} ControllerOptionsOverrides
* @typedef {import('./types').Factory} Factory
*/

/**
Expand All @@ -17,7 +18,7 @@ const Server = require('./endpoint/server')
* @returns {Factory}
*/
const createFactory = (options, overrides) => {
return new Factory(options, overrides)
return new DefaultFactory(options, overrides)
}

/**
Expand All @@ -27,7 +28,7 @@ const createFactory = (options, overrides) => {
* @returns {Promise<Controller>}
*/
const createController = (options) => {
const f = new Factory()
const f = new DefaultFactory()
return f.spawn(options)
}

Expand Down
7 changes: 5 additions & 2 deletions src/ipfsd-daemon.js
Expand Up @@ -26,7 +26,10 @@ function translateError (err) {
return err
}

/** @typedef {import("./types").ControllerOptions} ControllerOptions */
/**
* @typedef {import("./types").ControllerOptions} ControllerOptions
* @typedef {import("./types").Controller} Controller
*/

/**
* Controller for daemon nodes
Expand Down Expand Up @@ -113,7 +116,7 @@ class Daemon {
* Initialize a repo.
*
* @param {import('./types').InitOptions} [initOptions={}]
* @returns {Promise<Daemon>}
* @returns {Promise<Controller>}
*/
async init (initOptions = {}) {
this.initialized = await repoExists(this.path)
Expand Down
13 changes: 8 additions & 5 deletions src/ipfsd-in-proc.js
Expand Up @@ -148,22 +148,25 @@ class InProc {
}

/**
* Get the pid of the `ipfs daemon` process.
* Get the pid of the `ipfs daemon` process
*
* @returns {undefined}
* @returns {Promise<number>}
*/
pid () {
throw new Error('not implemented')
return Promise.reject(new Error('not implemented'))
}

/**
* Get the version of ipfs
*
* @returns {Promise<Object>}
* @returns {Promise<string>}
*/
async version () {
await this.setExec()
return this.api.version()

const { version } = await this.api.version()

return version
}
}

Expand Down
35 changes: 30 additions & 5 deletions src/types.d.ts
@@ -1,19 +1,37 @@

import { EventListener } from 'events'
import { EventEmitter } from 'events'
import { IPFS } from 'ipfs-core-types'
import { IDResult } from 'ipfs-core-types/src/root'

export interface Subprocess {
stderr: EventListener
stdout: EventListener
stderr: EventEmitter | null
stdout: EventEmitter | null
}

export interface API extends IPFS {
peerId: IDResult
apiHost: string
apiPort: string
gatewayHost: string
gatewayPort: string
grpcHost: string
grpcPort: string
}

export interface Controller {
init: () => Promise<Controller>
init: (options?: InitOptions) => Promise<Controller>
start: () => Promise<Controller>
stop: () => Promise<Controller>
cleanup: () => Promise<Controller>
pid: () => Promise<number>
version: () => Promise<string>
path: string
started: boolean
api: any
initialized: boolean
clean: boolean
api: API
subprocess?: Subprocess | null
opts: ControllerOptions
}

export interface RemoteState {
Expand Down Expand Up @@ -183,3 +201,10 @@ export interface ControllerOptionsOverrides {
go?: ControllerOptions
proc?: ControllerOptions
}

export interface Factory {
tmpDir: (options?: ControllerOptions) => Promise<string>
spawn: (options?: ControllerOptions) => Promise<Controller>
clean: () => Promise<void>
controllers: Controller[]
}
16 changes: 15 additions & 1 deletion test/controller.spec.js
Expand Up @@ -153,6 +153,18 @@ describe('Controller API', function () {
})
}
})

describe('should return a version', () => {
for (const opts of types) {
it(`type: ${opts.type} remote: ${Boolean(opts.remote)}`, async () => {
const ctl = await factory.spawn(opts)

const version = await ctl.version()

expect(version).to.be.a('string')
})
}
})
})

describe('start', () => {
Expand Down Expand Up @@ -325,8 +337,10 @@ describe('Controller API', function () {
await ctl.init()
await ctl.start()
await ctl.stop()
if (ctl.subprocess) {
if (ctl.subprocess && ctl.subprocess.stderr) {
expect(ctl.subprocess.stderr.listeners('data')).to.be.empty()
}
if (ctl.subprocess && ctl.subprocess.stdout) {
expect(ctl.subprocess.stdout.listeners('data')).to.be.empty()
}
})
Expand Down
11 changes: 9 additions & 2 deletions test/factory.spec.js
Expand Up @@ -6,6 +6,7 @@ const { isNode } = require('ipfs-utils/src/env')
const pathJoin = require('ipfs-utils/src/path-join')
const { createFactory } = require('../src')
const { UnixFS } = require('ipfs-unixfs')
const last = require('it-last')

const defaultOps = {
ipfsHttpModule: require('ipfs-http-client')
Expand Down Expand Up @@ -190,10 +191,16 @@ describe('`Factory spawn()` ', function () {
expect(node.api).to.exist()
expect(node.api.id).to.exist()

const { cid } = await node.api.add({ path: 'derp.txt', content: 'hello' }, {
const res = await last(node.api.addAll([{ path: 'derp.txt', content: 'hello' }], {
shardSplitThreshold: 0,
wrapWithDirectory: true
})
}))

if (!res) {
throw new Error('No result from ipfs.addAll')
}

const { cid } = res

const { value: dagNode } = await node.api.dag.get(cid)
const entry = UnixFS.unmarshal(dagNode.Data)
Expand Down

0 comments on commit afe87c2

Please sign in to comment.