Skip to content

Commit ee3a35a

Browse files
authored
feat: convert to typescript (#777)
Converts this module to typescript
1 parent d2c9df6 commit ee3a35a

26 files changed

+481
-661
lines changed

.aegir.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import { createServer } from './src/index.js'
21
import * as ipfsModule from 'ipfs'
32
import * as ipfsHttpModule from 'ipfs-http-client'
43
import * as goIpfsModule from 'go-ipfs'
54

65
/** @type {import('aegir').Options["build"]["config"]} */
7-
/*
8-
const esbuild = {
9-
inject: [path.join(__dirname, 'scripts/node-globals.js')],
10-
}
11-
*/
12-
export default {
6+
const config = {
137
bundlesize: {
148
maxSize: '35kB'
159
},
1610
test: {
17-
browser: {
18-
config: {
19-
//buildConfig: esbuild
20-
}
21-
},
2211
before: async () => {
12+
const { createServer } = await import('./dist/src/index.js')
13+
2314
const server = createServer(undefined, {
2415
ipfsModule,
2516
ipfsHttpModule
@@ -47,3 +38,5 @@ export default {
4738
}
4839
}
4940
}
41+
42+
export default config

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ logs
1111
*.log
1212

1313
coverage
14+
.coverage
1415

1516
# Runtime data
1617
pids

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# ipfsd-ctl <!-- omit in toc -->
22

3-
[![ipfs.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io)
4-
[![IRC](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
5-
[![Discord](https://img.shields.io/discord/806902334369824788?style=flat-square)](https://discord.gg/ipfs)
3+
[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
65
[![codecov](https://img.shields.io/codecov/c/github/ipfs/js-ipfsd-ctl.svg?style=flat-square)](https://codecov.io/gh/ipfs/js-ipfsd-ctl)
76
[![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)
87

package.json

+8-22
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,9 @@
2222
},
2323
"type": "module",
2424
"types": "./dist/src/index.d.ts",
25-
"typesVersions": {
26-
"*": {
27-
"*": [
28-
"*",
29-
"dist/*",
30-
"dist/src/*",
31-
"dist/src/*/index"
32-
],
33-
"src/*": [
34-
"*",
35-
"dist/*",
36-
"dist/src/*",
37-
"dist/src/*/index"
38-
]
39-
}
40-
},
4125
"files": [
4226
"src",
43-
"dist",
27+
"dist/src",
4428
"!dist/test",
4529
"!**/*.tsbuildinfo"
4630
],
@@ -142,11 +126,13 @@
142126
]
143127
},
144128
"scripts": {
129+
"clean": "aegir clean",
145130
"lint": "aegir lint",
131+
"dep-check": "aegir dep-check",
146132
"build": "aegir build",
147133
"test": "aegir test",
148-
"test:node": "aegir test -t node",
149-
"test:chrome": "aegir test -t browser",
134+
"test:node": "aegir test -t node --cov",
135+
"test:chrome": "aegir test -t browser --cov",
150136
"test:firefox": "aegir test -t browser -- --browser firefox",
151137
"release": "aegir release"
152138
},
@@ -176,9 +162,9 @@
176162
"util": "^0.12.4"
177163
},
178164
"browser": {
179-
"./src/endpoint/server.js": "./src/endpoint/server.browser.js",
180-
"./src/utils.js": "./src/utils.browser.js",
181-
"./src/ipfsd-daemon.js": "./src/ipfsd-client.js",
165+
"./dist/src/endpoint/server.js": "./dist/src/endpoint/server.browser.js",
166+
"./dist/src/utils.js": "./dist/src/utils.browser.js",
167+
"./dist/src/ipfsd-daemon.js": "./dist/src/ipfsd-client.js",
182168
"go-ipfs": false
183169
},
184170
"jsdelivr": "dist/index.min.js",

src/config.js renamed to src/config.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { isBrowser, isWebWorker } from 'wherearewe'
2+
import type { NodeType } from './index.js'
23

3-
/**
4-
* @param {object} args
5-
* @param {import('./types').NodeType} args.type
6-
*/
7-
export default ({ type }) => {
8-
/** @type {string[]} */
9-
let swarm
4+
export interface ConfigInit {
5+
type?: NodeType
6+
}
7+
8+
export default (init: ConfigInit) => {
9+
const { type } = init
10+
let swarm: string[]
1011

1112
// from the browser tell remote nodes to listen over WS
1213
if (type !== 'proc' && (isBrowser || isWebWorker)) {

src/endpoint/routes.js renamed to src/endpoint/routes.ts

+23-38
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import Joi from 'joi'
33
import boom from '@hapi/boom'
44
import { logger } from '@libp2p/logger'
55
import { tmpDir } from '../utils.js'
6-
7-
/**
8-
* @typedef {import('../types').Factory} Factory
9-
*/
6+
import type { Server } from '@hapi/hapi'
7+
import type { Factory } from '../index.js'
108

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

@@ -18,12 +16,9 @@ const routeOptions = {
1816
}
1917
}
2018

21-
/**
22-
* @param {Error & { stdout?: string }} err
23-
*/
24-
const badRequest = err => {
19+
const badRequest = (err: Error & { stdout?: string }) => {
2520
let msg
26-
if (err.stdout) {
21+
if (err.stdout != null) {
2722
msg = err.stdout + ' - ' + err.message
2823
} else {
2924
msg = err.message
@@ -32,27 +27,17 @@ const badRequest = err => {
3227
throw boom.badRequest(msg)
3328
}
3429

35-
/**
36-
* @type {Record<string, any>}
37-
*/
38-
const nodes = {}
39-
40-
/**
41-
* @namespace EndpointServerRoutes
42-
* @ignore
43-
* @param {import('@hapi/hapi').Server} server
44-
* @param {() => Factory | Promise<Factory>} createFactory
45-
* @returns {void}
46-
*/
47-
export default (server, createFactory) => {
30+
const nodes: Record<string, any> = {}
31+
32+
export default (server: Server, createFactory: () => Factory | Promise<Factory>): void => {
4833
server.route({
4934
method: 'GET',
5035
path: '/util/tmp-dir',
5136
handler: async (request) => {
52-
const type = request.query.type || 'go'
37+
const type = request.query.type ?? 'go'
5338
try {
5439
return { tmpDir: await tmpDir(type) }
55-
} catch (/** @type {any} */ err) {
40+
} catch (err: any) {
5641
badRequest(err)
5742
}
5843
}
@@ -66,7 +51,7 @@ export default (server, createFactory) => {
6651

6752
try {
6853
return { version: await nodes[id].version() }
69-
} catch (/** @type {any} */ err) {
54+
} catch (err: any) {
7055
badRequest(err)
7156
}
7257
},
@@ -77,25 +62,25 @@ export default (server, createFactory) => {
7762
method: 'POST',
7863
path: '/spawn',
7964
handler: async (request) => {
80-
const opts = request.payload || {}
65+
const opts = request.payload ?? {}
8166
try {
8267
const ipfsd = await createFactory()
8368
const id = nanoid()
8469
// @ts-expect-error opts is a json object
8570
nodes[id] = await ipfsd.spawn(opts)
8671
return {
8772
id: id,
88-
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
89-
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
90-
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : '',
73+
apiAddr: nodes[id].apiAddr?.toString(),
74+
gatewayAddr: nodes[id].gatewayAddr?.toString(),
75+
grpcAddr: nodes[id].grpcAddr?.toString(),
9176
initialized: nodes[id].initialized,
9277
started: nodes[id].started,
9378
disposable: nodes[id].disposable,
9479
env: nodes[id].env,
9580
path: nodes[id].path,
9681
clean: nodes[id].clean
9782
}
98-
} catch (/** @type {any} */ err) {
83+
} catch (err: any) {
9984
badRequest(err)
10085
}
10186
}
@@ -109,15 +94,15 @@ export default (server, createFactory) => {
10994
path: '/init',
11095
handler: async (request) => {
11196
const id = request.query.id
112-
const payload = request.payload || {}
97+
const payload = request.payload ?? {}
11398

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

117102
return {
118103
initialized: nodes[id].initialized
119104
}
120-
} catch (/** @type {any} */ err) {
105+
} catch (err: any) {
121106
badRequest(err)
122107
}
123108
},
@@ -137,11 +122,11 @@ export default (server, createFactory) => {
137122
await nodes[id].start()
138123

139124
return {
140-
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
141-
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
142-
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : ''
125+
apiAddr: nodes[id].apiAddr?.toString(),
126+
gatewayAddr: nodes[id].gatewayAddr?.toString(),
127+
grpcAddr: nodes[id].grpcAddr?.toString()
143128
}
144-
} catch (/** @type {any} */ err) {
129+
} catch (err: any) {
145130
badRequest(err)
146131
}
147132
},
@@ -163,7 +148,7 @@ export default (server, createFactory) => {
163148
await nodes[id].cleanup()
164149

165150
return h.response().code(200)
166-
} catch (/** @type {any} */ err) {
151+
} catch (err: any) {
167152
badRequest(err)
168153
}
169154
},
@@ -183,7 +168,7 @@ export default (server, createFactory) => {
183168
await nodes[id].stop()
184169

185170
return h.response().code(200)
186-
} catch (/** @type {any} */ err) {
171+
} catch (err: any) {
187172
badRequest(err)
188173
}
189174
},

src/endpoint/server.browser.js renamed to src/endpoint/server.browser.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
/* eslint-disable no-console */
22

3+
import type { ServerInit } from './server.js'
4+
35
/**
4-
* Creates an instance of Server.
5-
*
6-
* @class
6+
* Creates an instance of Server
77
*/
88
class Server {
9-
/**
10-
* @class
11-
* @param {object} options
12-
* @param {number} [options.port=43134] - Server port.
13-
* @param {Function} createNode
14-
*/
15-
constructor (options, createNode) {
16-
options = options || { port: 43134 }
9+
private readonly options: ServerInit
10+
public port: number
11+
public host: string
12+
13+
constructor (options: ServerInit = { port: 43134, host: 'localhost' }) {
14+
this.options = options
15+
this.port = this.options.port ?? 43134
16+
this.host = this.options.host ?? 'localhost'
1717

18-
/** @type {*} */
19-
this.server = null
20-
this.port = options.port
21-
this.createNode = createNode
2218
console.warn('Server not implemented in the browser')
2319
}
2420

src/endpoint/server.js

-66
This file was deleted.

0 commit comments

Comments
 (0)