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

feat: convert to typescript #777

Merged
merged 4 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 5 additions & 12 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { createServer } from './src/index.js'
import * as ipfsModule from 'ipfs'
import * as ipfsHttpModule from 'ipfs-http-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
Expand Down Expand Up @@ -47,3 +38,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
5 changes: 2 additions & 3 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
30 changes: 8 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 Down Expand Up @@ -176,9 +162,9 @@
"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 { NodeType } from './index.js'

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

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(),
Comment on lines +73 to +75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're changing the results from being '' (empty string) when undefined, to being undefined. Is this okay?

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()
Comment on lines +125 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're changing the results from being '' (empty string) when undefined, to being undefined. Is this okay?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it's a bit better like as these fields will be stripped out on JSON encode now as they're undefined instead of being an empty string which is not a valid string representation of a multiaddr.

}
} 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
66 changes: 0 additions & 66 deletions src/endpoint/server.js

This file was deleted.

Loading