Skip to content

feat: strict TypeScript configuration #2010

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

Closed
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
101 changes: 101 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@
"@esm-bundle/chai": "^4.3.4",
"@release-it/conventional-changelog": "^7.0.2",
"@types/chai": "^4.3.20",
"@types/concat-stream": "^2.0.3",
"@types/debug": "^4.1.12",
"@types/minimist": "^1.2.5",
Copy link
Member

Choose a reason for hiding this comment

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

I think we could drop minimis in favor of nodejs parseArgs but maybe we can do that in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, just make sure that this does not break browsers implementation. I think there is a global dependency review to be done on the overall code. Let's add this to a roadmap if there is one. 👍

Copy link
Member

Choose a reason for hiding this comment

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

just make sure that this does not break browsers implementation

Minimist isn't used on browser side but just on /bin files that can only be run from nodejs :)

"@types/node": "^20.17.16",
"@types/rfdc": "^1.1.0",
"@types/sinon": "^17.0.3",
"@types/split2": "^4.2.3",
"@types/tape": "^5.8.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
Expand Down
6 changes: 4 additions & 2 deletions src/bin/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* See LICENSE for more information
*/
import path from 'path'
// @ts-expect-error - There is no types available for this library.
import Commist from 'commist'
// @ts-expect-error - There is no types available for this library.
import help from 'help-me'
import publish from './pub'
import subscribe from './sub'
import publish from './pub.js'
import subscribe from './sub.js'
Comment on lines +14 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Best practice to avoid confusion between folders and files, there are also other more specific requirements from Node.js and browsers if using .mjs extension.


// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require('../../package.json').version
Expand Down
91 changes: 42 additions & 49 deletions src/bin/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,42 @@ import { Writable } from 'readable-stream'
import path from 'path'
import fs from 'fs'
import concat from 'concat-stream'
// @ts-expect-error - There is no types available for this library.
import help from 'help-me'

import minimist, { type ParsedArgs } from 'minimist'
import split2 from 'split2'
import { connect } from '../mqtt'
import { type IClientOptions, type IClientPublishOptions } from 'src/lib/client'
import { connect } from '../mqtt.js'
import { pipeline } from 'stream'

const helpMe = help({
dir: path.join(__dirname, '../../', 'help'),
dir: path.join(import.meta.dirname, '../../', 'help'),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not strictly necessary but enables migration to ESM.

})

function send(args: ParsedArgs) {
const client = connect(args as IClientOptions)
const client = connect(args)
client.on('connect', () => {
client.publish(
args.topic,
args.message,
args as IClientPublishOptions,
(err) => {
if (err) {
console.warn(err)
}
client.end()
},
)
client.publish(args['topic'], args['message'], args, (err: unknown) => {
if (err !== undefined) {
console.warn(err)
}
client.end()
})
})
client.on('error', (err) => {
client.on('error', (err: unknown) => {
console.warn(err)
client.end()
})
}

function multisend(args: ParsedArgs) {
const client = connect(args as IClientOptions)
const client = connect(args)
const sender = new Writable({
objectMode: true,
})
// @ts-expect-error - enc is not use but might be used in other overloads. @TODO: This would need to be looked at.
sender._write = (line, enc, cb) => {
client.publish(
args.topic,
line.trim(),
args as IClientPublishOptions,
cb,
)
client.publish(args['topic'], line.trim(), args, cb)
}

client.on('connect', () => {
Expand Down Expand Up @@ -101,65 +92,67 @@ export default function start(args: string[]) {
},
})

if (parsedArgs.help) {
if (parsedArgs['help']) {
return helpMe.toStdout('publish')
}

if (parsedArgs.key) {
parsedArgs.key = fs.readFileSync(parsedArgs.key)
if (parsedArgs['key']) {
parsedArgs['key'] = fs.readFileSync(parsedArgs['key'])
}

if (parsedArgs.cert) {
parsedArgs.cert = fs.readFileSync(parsedArgs.cert)
if (parsedArgs['cert']) {
parsedArgs['cert'] = fs.readFileSync(parsedArgs['cert'])
}

if (parsedArgs.ca) {
parsedArgs.ca = fs.readFileSync(parsedArgs.ca)
if (parsedArgs['ca']) {
parsedArgs['ca'] = fs.readFileSync(parsedArgs['ca'])
}

if (parsedArgs.key && parsedArgs.cert && !parsedArgs.protocol) {
parsedArgs.protocol = 'mqtts'
if (parsedArgs['key'] && parsedArgs['cert'] && !parsedArgs['protocol']) {
parsedArgs['protocol'] = 'mqtts'
}

if (parsedArgs.port) {
if (typeof parsedArgs.port !== 'number') {
if (parsedArgs['port']) {
if (typeof parsedArgs['port'] !== 'number') {
console.warn(
"# Port: number expected, '%s' was given.",
typeof parsedArgs.port,
typeof parsedArgs['port'],
)
return
}
}

if (parsedArgs['will-topic']) {
parsedArgs.will = {}
parsedArgs.will.topic = parsedArgs['will-topic']
parsedArgs.will.payload = parsedArgs['will-message']
parsedArgs.will.qos = parsedArgs['will-qos']
parsedArgs.will.retain = parsedArgs['will-retain']
parsedArgs['will'] = {}
parsedArgs['will'].topic = parsedArgs['will-topic']
parsedArgs['will'].payload = parsedArgs['will-message']
parsedArgs['will'].qos = parsedArgs['will-qos']
parsedArgs['will'].retain = parsedArgs['will-retain']
Comment on lines +126 to +130
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of these, and every access made that way can be easily resolved by using typeguards in a later PR.

}

if (parsedArgs.insecure) {
parsedArgs.rejectUnauthorized = false
if (parsedArgs['insecure']) {
parsedArgs['rejectUnauthorized'] = false
}

parsedArgs.topic = (parsedArgs.topic || parsedArgs._.shift())?.toString()
parsedArgs.message = (
parsedArgs.message || parsedArgs._.shift()
parsedArgs['topic'] = (
parsedArgs['topic'] || parsedArgs._.shift()
)?.toString()
parsedArgs['message'] = (
parsedArgs['message'] || parsedArgs._.shift()
)?.toString()

if (!parsedArgs.topic) {
if (!parsedArgs['topic']) {
console.error('missing topic\n')
return helpMe.toStdout('publish')
}

if (parsedArgs.stdin) {
if (parsedArgs.multiline) {
if (parsedArgs['stdin']) {
if (parsedArgs['multiline']) {
multisend(parsedArgs)
} else {
process.stdin.pipe(
concat((data) => {
parsedArgs.message = data
parsedArgs['message'] = data
send(parsedArgs)
}),
)
Expand Down
Loading