From afe87c2c30737041d49a838514052e062fca0267 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 30 Jul 2021 20:20:21 +0100 Subject: [PATCH] fix: types again (#652) More comprehensive types available. Also turn factory into an interface. --- package.json | 3 +++ src/factory.js | 2 +- src/index.js | 13 +++++++------ src/ipfsd-daemon.js | 7 +++++-- src/ipfsd-in-proc.js | 13 ++++++++----- src/types.d.ts | 35 ++++++++++++++++++++++++++++++----- test/controller.spec.js | 16 +++++++++++++++- test/factory.spec.js | 11 +++++++++-- 8 files changed, 78 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index b9856a1b..5b29d931 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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": { diff --git a/src/factory.js b/src/factory.js index e272fdf8..a3fa0795 100644 --- a/src/factory.js +++ b/src/factory.js @@ -106,7 +106,7 @@ class Factory { * Spawn an IPFSd Controller * * @param {ControllerOptions} options - * @returns {Promise} + * @returns {Promise} */ async spawn (options = { }) { const type = options.type || this.opts.type || 'go' diff --git a/src/index.js b/src/index.js index 1bf46134..c21f4697 100644 --- a/src/index.js +++ b/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 */ /** @@ -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) } /** @@ -27,7 +28,7 @@ const createFactory = (options, overrides) => { * @returns {Promise} */ const createController = (options) => { - const f = new Factory() + const f = new DefaultFactory() return f.spawn(options) } diff --git a/src/ipfsd-daemon.js b/src/ipfsd-daemon.js index d273d059..241480e2 100644 --- a/src/ipfsd-daemon.js +++ b/src/ipfsd-daemon.js @@ -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 @@ -113,7 +116,7 @@ class Daemon { * Initialize a repo. * * @param {import('./types').InitOptions} [initOptions={}] - * @returns {Promise} + * @returns {Promise} */ async init (initOptions = {}) { this.initialized = await repoExists(this.path) diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index bb356c5e..ceb4b49e 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -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} */ pid () { - throw new Error('not implemented') + return Promise.reject(new Error('not implemented')) } /** * Get the version of ipfs * - * @returns {Promise} + * @returns {Promise} */ async version () { await this.setExec() - return this.api.version() + + const { version } = await this.api.version() + + return version } } diff --git a/src/types.d.ts b/src/types.d.ts index 4985711a..4ee4cc2b 100644 --- a/src/types.d.ts +++ b/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 + init: (options?: InitOptions) => Promise start: () => Promise stop: () => Promise + cleanup: () => Promise + pid: () => Promise + version: () => Promise path: string started: boolean - api: any + initialized: boolean + clean: boolean + api: API subprocess?: Subprocess | null + opts: ControllerOptions } export interface RemoteState { @@ -183,3 +201,10 @@ export interface ControllerOptionsOverrides { go?: ControllerOptions proc?: ControllerOptions } + +export interface Factory { + tmpDir: (options?: ControllerOptions) => Promise + spawn: (options?: ControllerOptions) => Promise + clean: () => Promise + controllers: Controller[] +} diff --git a/test/controller.spec.js b/test/controller.spec.js index 4ab4f55d..f09eb975 100644 --- a/test/controller.spec.js +++ b/test/controller.spec.js @@ -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', () => { @@ -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() } }) diff --git a/test/factory.spec.js b/test/factory.spec.js index 79caae21..ede2b062 100644 --- a/test/factory.spec.js +++ b/test/factory.spec.js @@ -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') @@ -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)