Skip to content

Commit

Permalink
Merge branch 'master' into fix/pass-config-show-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Oct 5, 2022
2 parents a278d5e + 9538c1d commit ac1316f
Show file tree
Hide file tree
Showing 27 changed files with 629 additions and 734 deletions.
25 changes: 10 additions & 15 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import { createServer } from './src/index.js'
import * as ipfsModule from 'ipfs'
import * as ipfsHttpModule from 'ipfs-http-client'
import * as kuboRpcModule from 'kubo-rpc-client'
import * as goIpfsModule from 'go-ipfs'

/** @type {import('aegir').Options["build"]["config"]} */
/*
const esbuild = {
inject: [path.join(__dirname, 'scripts/node-globals.js')],
}
*/
export default {
const config = {
bundlesize: {
maxSize: '35kB'
},
test: {
browser: {
config: {
//buildConfig: esbuild
}
},
before: async () => {
const { createServer } = await import('./dist/src/index.js')

const server = createServer(undefined, {
ipfsModule,
ipfsHttpModule
}, {
go: {
ipfsBin: goIpfsModule.path()
ipfsBin: goIpfsModule.path(),
kuboRpcModule
},
js: {
ipfsBin: ipfsModule.path()
ipfsBin: ipfsModule.path(),
ipfsHttpModule
}
}
)
Expand All @@ -47,3 +40,5 @@ export default {
}
}
}

export default config
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ logs
*.log

coverage
.coverage

# Runtime data
pids
Expand Down
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## [12.1.1](https://github.com/ipfs/js-ipfsd-ctl/compare/v12.1.0...v12.1.1) (2022-10-05)


### Bug Fixes

* update export path to point to correct file ([#778](https://github.com/ipfs/js-ipfsd-ctl/issues/778)) ([ecdda55](https://github.com/ipfs/js-ipfsd-ctl/commit/ecdda5547491e9826568bb8c3181121de38b5362))

## [12.1.0](https://github.com/ipfs/js-ipfsd-ctl/compare/v12.0.3...v12.1.0) (2022-10-04)


### Features

* convert to typescript ([#777](https://github.com/ipfs/js-ipfsd-ctl/issues/777)) ([ee3a35a](https://github.com/ipfs/js-ipfsd-ctl/commit/ee3a35a28c7a435c49da02d4e03d29c4520e5b55))

## [12.0.3](https://github.com/ipfs/js-ipfsd-ctl/compare/v12.0.2...v12.0.3) (2022-09-21)


### Dependencies

* update @multiformats/multiaddr to 11.0.0 ([#775](https://github.com/ipfs/js-ipfsd-ctl/issues/775)) ([087e80c](https://github.com/ipfs/js-ipfsd-ctl/commit/087e80ce905d846f6047f7528ae9a2468afa583e))

## [12.0.2](https://github.com/ipfs/js-ipfsd-ctl/compare/v12.0.1...v12.0.2) (2022-09-02)


Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# ipfsd-ctl <!-- omit in toc -->

[![ipfs.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io)
[![IRC](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Discord](https://img.shields.io/discord/806902334369824788?style=flat-square)](https://discord.gg/ipfs)
[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-ipfsd-ctl.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-ipfsd-ctl)
[![CI](https://img.shields.io/github/workflow/status/ipfs/js-ipfsd-ctl/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/ipfs/js-ipfsd-ctl/actions/workflows/js-test-and-release.yml)

Expand Down Expand Up @@ -62,17 +61,15 @@ $ npm i ipfsd-ctl

Version 1.0.0 changed a bit the api and the options methods take so please read the documentation below.

Please ensure your project also has dependencies on `ipfs`, `ipfs-http-client` and `go-ipfs`.
Please ensure your project also has dependencies on `ipfs`, `ipfs-http-client`, `kubo-rpc-client`, and `go-ipfs`.

```sh
npm install --save ipfs
npm install --save ipfs-http-client
npm install --save go-ipfs
npm install --save ipfs ipfs-http-client go-ipfs kubo-rpc-client
```

If you are only going to use the `go` implementation of IPFS, you can skip installing the `js` implementation and vice versa, though both will require the `ipfs-http-client` module.
If you are only going to use the `go` implementation of IPFS, you can skip installing the `js` implementation and `ipfs-http-client` module. (e.g. `npm i --save go-ipfs kubo-rpc-client`)

If you are only using the `proc` type in-process IPFS node, you can skip installing `go-ipfs` and `ipfs-http-client`.
If you are only using the `proc` type in-process IPFS node, you can skip installing `go-ipfs` and `ipfs-http-client`. (e.g. `npm i --save ipfs`)

> You also need to explicitly defined the options `ipfsBin`, `ipfsModule` and `ipfsHttpModule` according to your needs. Check [ControllerOptions](#controlleroptions) and [ControllerOptionsOverrides](#controlleroptionsoverrides) for more information.
Expand Down
49 changes: 18 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ipfsd-ctl",
"version": "12.0.2",
"version": "12.1.1",
"description": "Spawn IPFS Daemons, JS or Go",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ipfs/js-ipfsd-ctl#readme",
Expand All @@ -22,32 +22,16 @@
},
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"dist/src",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.js"
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
Expand Down Expand Up @@ -142,11 +126,13 @@
]
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:chrome": "aegir test -t browser",
"test:node": "aegir test -t node --cov",
"test:chrome": "aegir test -t browser --cov",
"test:firefox": "aegir test -t browser -- --browser firefox",
"release": "aegir release"
},
Expand All @@ -155,30 +141,31 @@
"@hapi/hapi": "^20.0.0",
"@libp2p/interface-peer-id": "^1.0.4",
"@libp2p/logger": "^2.0.0",
"@multiformats/multiaddr": "^10.1.8",
"@multiformats/multiaddr": "^11.0.0",
"execa": "^6.1.0",
"ipfs-utils": "^9.0.1",
"joi": "^17.2.1",
"merge-options": "^3.0.1",
"nanoid": "^4.0.0",
"p-wait-for": "^4.1.0",
"p-wait-for": "^5.0.0",
"temp-write": "^5.0.0",
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@types/hapi__hapi": "^20.0.9",
"aegir": "^37.0.15",
"go-ipfs": "^0.13.0",
"ipfs": "^0.63.5",
"ipfs-client": "^0.8.3",
"ipfs-core-types": "^0.11.1",
"ipfs-http-client": "^57.0.3",
"go-ipfs": "^0.15.0",
"ipfs": "^0.64.0",
"ipfs-client": "^0.9.0",
"ipfs-core-types": "^0.12.0",
"ipfs-http-client": "^58.0.0",
"kubo-rpc-client": "^1.0.1",
"util": "^0.12.4"
},
"browser": {
"./src/endpoint/server.js": "./src/endpoint/server.browser.js",
"./src/utils.js": "./src/utils.browser.js",
"./src/ipfsd-daemon.js": "./src/ipfsd-client.js",
"./dist/src/endpoint/server.js": "./dist/src/endpoint/server.browser.js",
"./dist/src/utils.js": "./dist/src/utils.browser.js",
"./dist/src/ipfsd-daemon.js": "./dist/src/ipfsd-client.js",
"go-ipfs": false
},
"jsdelivr": "dist/index.min.js",
Expand Down
15 changes: 8 additions & 7 deletions src/config.js → src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { isBrowser, isWebWorker } from 'wherearewe'
import type { ControllerType } from './index.js'

/**
* @param {object} args
* @param {import('./types').NodeType} args.type
*/
export default ({ type }) => {
/** @type {string[]} */
let swarm
export interface ConfigInit {
type?: ControllerType
}

export default (init: ConfigInit) => {
const { type } = init
let swarm: string[]

// from the browser tell remote nodes to listen over WS
if (type !== 'proc' && (isBrowser || isWebWorker)) {
Expand Down
61 changes: 23 additions & 38 deletions src/endpoint/routes.js → src/endpoint/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import Joi from 'joi'
import boom from '@hapi/boom'
import { logger } from '@libp2p/logger'
import { tmpDir } from '../utils.js'

/**
* @typedef {import('../types').Factory} Factory
*/
import type { Server } from '@hapi/hapi'
import type { Factory } from '../index.js'

const debug = logger('ipfsd-ctl:routes')

Expand All @@ -18,12 +16,9 @@ const routeOptions = {
}
}

/**
* @param {Error & { stdout?: string }} err
*/
const badRequest = err => {
const badRequest = (err: Error & { stdout?: string }) => {
let msg
if (err.stdout) {
if (err.stdout != null) {
msg = err.stdout + ' - ' + err.message
} else {
msg = err.message
Expand All @@ -32,27 +27,17 @@ const badRequest = err => {
throw boom.badRequest(msg)
}

/**
* @type {Record<string, any>}
*/
const nodes = {}

/**
* @namespace EndpointServerRoutes
* @ignore
* @param {import('@hapi/hapi').Server} server
* @param {() => Factory | Promise<Factory>} createFactory
* @returns {void}
*/
export default (server, createFactory) => {
const nodes: Record<string, any> = {}

export default (server: Server, createFactory: () => Factory | Promise<Factory>): void => {
server.route({
method: 'GET',
path: '/util/tmp-dir',
handler: async (request) => {
const type = request.query.type || 'go'
const type = request.query.type ?? 'go'
try {
return { tmpDir: await tmpDir(type) }
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
}
Expand All @@ -66,7 +51,7 @@ export default (server, createFactory) => {

try {
return { version: await nodes[id].version() }
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
},
Expand All @@ -77,25 +62,25 @@ export default (server, createFactory) => {
method: 'POST',
path: '/spawn',
handler: async (request) => {
const opts = request.payload || {}
const opts = request.payload ?? {}
try {
const ipfsd = await createFactory()
const id = nanoid()
// @ts-expect-error opts is a json object
nodes[id] = await ipfsd.spawn(opts)
return {
id: id,
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : '',
apiAddr: nodes[id].apiAddr?.toString(),
gatewayAddr: nodes[id].gatewayAddr?.toString(),
grpcAddr: nodes[id].grpcAddr?.toString(),
initialized: nodes[id].initialized,
started: nodes[id].started,
disposable: nodes[id].disposable,
env: nodes[id].env,
path: nodes[id].path,
clean: nodes[id].clean
}
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
}
Expand All @@ -109,15 +94,15 @@ export default (server, createFactory) => {
path: '/init',
handler: async (request) => {
const id = request.query.id
const payload = request.payload || {}
const payload = request.payload ?? {}

try {
await nodes[id].init(payload)

return {
initialized: nodes[id].initialized
}
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
},
Expand All @@ -137,11 +122,11 @@ export default (server, createFactory) => {
await nodes[id].start()

return {
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : ''
apiAddr: nodes[id].apiAddr?.toString(),
gatewayAddr: nodes[id].gatewayAddr?.toString(),
grpcAddr: nodes[id].grpcAddr?.toString()
}
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
},
Expand All @@ -163,7 +148,7 @@ export default (server, createFactory) => {
await nodes[id].cleanup()

return h.response().code(200)
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
},
Expand All @@ -183,7 +168,7 @@ export default (server, createFactory) => {
await nodes[id].stop()

return h.response().code(200)
} catch (/** @type {any} */ err) {
} catch (err: any) {
badRequest(err)
}
},
Expand Down
Loading

0 comments on commit ac1316f

Please sign in to comment.