Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add js kubo rpc client #1

Merged
merged 25 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
20ea2f9
feat: add js-kubo-rpc-client
SgtPooki Aug 19, 2022
cb0e10b
chore: update kubo-rpc-client package name
SgtPooki Sep 6, 2022
7fa9e7e
fix: use cross-env to set USE_KUBO_JS
SgtPooki Sep 7, 2022
3b775f0
Merge branch 'ipfs:master' into feature/add-js-kubo-rpc-client
SgtPooki Sep 19, 2022
ee3b36a
fix: Remove USE_KUBO_JS env var
SgtPooki Sep 19, 2022
60c2c84
chore(deps): remove cross-env
SgtPooki Sep 20, 2022
004ebc6
docs(readme): update npm install cmds for using kubo-rpc-client
SgtPooki Sep 21, 2022
b828341
chore: log whether kubo-rpc-client or ipfs-http-client is in use
SgtPooki Sep 21, 2022
e4e8bb5
chore(tests): use kubo-rpc-client for commmunication with go controllers
SgtPooki Sep 21, 2022
3db2fb5
Merge branch 'master' into feature/add-js-kubo-rpc-client
SgtPooki Sep 21, 2022
ab88fed
chore(lint): fix lint failure
SgtPooki Sep 21, 2022
0ed3917
fix: addCorrectRpcModule returns the additionalOpts object
SgtPooki Sep 21, 2022
9eb0b51
docs(readme): correct kubo-rpc-client name
SgtPooki Sep 22, 2022
8de4a2d
fix: use correct logger namespace convention
SgtPooki Sep 22, 2022
68673b8
fix: move kubo-rpc-client to devDeps
SgtPooki Sep 22, 2022
48adc63
fix: support generics for Controller type
SgtPooki Sep 22, 2022
cb4c373
chore(deps): use kubo-rpc-client@1.0.1
SgtPooki Sep 23, 2022
5783286
feat!: convert to typescript
achingbrain Sep 29, 2022
2f98a0b
chore: fix linting
achingbrain Sep 30, 2022
3a78c13
chore: fix tests
achingbrain Sep 30, 2022
feadf53
Merge branch 'feature/add-js-kubo-rpc-client' into feat/convert-to-ty…
SgtPooki Sep 30, 2022
86aec67
chore: apply suggestions from code review
achingbrain Oct 1, 2022
919e03f
fix: resolve issues with js-kubo-rpc-client in typescript
SgtPooki Oct 3, 2022
0635198
fix: use kuboRpcModule for go in factory.spec.ts
SgtPooki Oct 3, 2022
d1fa4c0
Merge branch 'feat/convert-to-typescript' into feature/add-js-kubo-rp…
SgtPooki Oct 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
32 changes: 10 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,9 @@
},
"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"
],
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 @@ -166,19 +152,21 @@
"wherearewe": "^2.0.1"
},
"devDependencies": {
"@libp2p/interfaces": "^3.0.3",
"@types/hapi__hapi": "^20.0.9",
"aegir": "^37.0.15",
"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
26 changes: 11 additions & 15 deletions src/endpoint/server.browser.js → src/endpoint/server.browser.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
/* eslint-disable no-console */

import type { ServerInit } from './server.js'

/**
* Creates an instance of Server.
*
* @class
* Creates an instance of Server
*/
class Server {
/**
* @class
* @param {object} options
* @param {number} [options.port=43134] - Server port.
* @param {Function} createNode
*/
constructor (options, createNode) {
options = options || { port: 43134 }
private readonly options: ServerInit
public port: number
public host: string

constructor (options: ServerInit = { port: 43134, host: 'localhost' }) {
this.options = options
this.port = this.options.port ?? 43134
this.host = this.options.host ?? 'localhost'

/** @type {*} */
this.server = null
this.port = options.port
this.createNode = createNode
console.warn('Server not implemented in the browser')
}

Expand Down
Loading