-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const version = require('../../package.json').version | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}), | ||
) | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minimist isn't used on browser side but just on
/bin
files that can only be run from nodejs :)