Skip to content

Commit eeda24e

Browse files
chore(types): migrate module resolution and module types (#2012)
* chore: Added a first iteration of TypeScript rules to migrate module resolution and module types. * chore: Extended tsconfig/node20. * chore: Attempted to resolve issues due to @tsconfig/node20 extension. * chore: Removed a comment.
1 parent 8f0f4d4 commit eeda24e

File tree

7 files changed

+57
-36
lines changed

7 files changed

+57
-36
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"devDependencies": {
132132
"@esm-bundle/chai": "^4.3.4",
133133
"@release-it/conventional-changelog": "^7.0.2",
134+
"@tsconfig/node20": "^20.1.6",
134135
"@types/chai": "^4.3.20",
135136
"@types/node": "^20.17.16",
136137
"@types/sinon": "^17.0.3",

src/lib/connect/ali.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Buffer } from 'buffer'
22
import { Transform } from 'readable-stream'
3-
import { type StreamBuilder } from '../shared'
3+
import { type IStream, type StreamBuilder } from '../shared'
44
import { type IClientOptions } from '../client'
55
import type MqttClient from '../client'
66
import { BufferedDuplex } from '../BufferedDuplex'
@@ -75,11 +75,13 @@ function bindEventHandler() {
7575
} else {
7676
const reader = new FileReader()
7777
reader.addEventListener('load', () => {
78-
let data = reader.result
78+
if (reader.result instanceof ArrayBuffer) {
79+
proxy.push(Buffer.from(reader.result))
7980

80-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
81-
else data = Buffer.from(data, 'utf8')
82-
proxy.push(data)
81+
return
82+
}
83+
84+
proxy.push(Buffer.from(reader.result, 'utf-8'))
8385
})
8486
reader.readAsArrayBuffer(res.data)
8587
}
@@ -95,7 +97,7 @@ function bindEventHandler() {
9597
})
9698
}
9799

98-
const buildStream: StreamBuilder = (client, opts) => {
100+
const buildStream: StreamBuilder = (client, opts): IStream => {
99101
opts.hostname = opts.hostname || opts.host
100102

101103
if (!opts.hostname) {

src/lib/connect/tls.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tls, { type TLSSocket } from 'tls'
1+
import { type TLSSocket, connect as tlsConnect } from 'tls'
22
import net from 'net'
33
import _debug from 'debug'
44
import { type StreamBuilder } from '../shared'
@@ -10,16 +10,18 @@ const debug = _debug('mqttjs:tls')
1010
function connect(opts: IClientOptions): TLSSocket {
1111
const { host, port, socksProxy, ...rest } = opts
1212

13-
return tls.connect(
14-
socksProxy
15-
? {
16-
...rest,
17-
socket: openSocks(host, port, socksProxy, {
18-
timeout: opts.socksTimeout,
19-
}),
20-
}
21-
: opts,
22-
)
13+
if (socksProxy !== undefined) {
14+
const socket = openSocks(host, port, socksProxy, {
15+
timeout: opts.socksTimeout,
16+
})
17+
18+
return tlsConnect({
19+
...rest,
20+
socket,
21+
})
22+
}
23+
24+
return tlsConnect(opts)
2325
}
2426

2527
const buildStream: StreamBuilder = (client, opts) => {

src/lib/connect/ws.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Buffer } from 'buffer'
22
import Ws, { type ClientOptions } from 'ws'
33
import _debug from 'debug'
4-
import { type DuplexOptions, Transform } from 'readable-stream'
5-
import { type StreamBuilder } from '../shared'
4+
import { Transform } from 'readable-stream'
5+
import { type IStream, type StreamBuilder } from '../shared'
66
import isBrowser from '../is-browser'
77
import { type IClientOptions } from '../client'
88
import type MqttClient from '../client'
@@ -138,18 +138,16 @@ function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
138138
return socket
139139
}
140140

141-
const streamBuilder: StreamBuilder = (client, opts) => {
141+
const streamBuilder: StreamBuilder = (client, opts): IStream => {
142142
debug('streamBuilder')
143143
const options = setDefaultOpts(opts)
144144

145145
options.hostname = options.hostname || options.host || 'localhost'
146146

147147
const url = buildUrl(options, client)
148148
const socket = createWebSocket(client, url, options)
149-
const webSocketStream = Ws.createWebSocketStream(
150-
socket,
151-
options.wsOptions as DuplexOptions,
152-
)
149+
// @ts-expect-error - This is a type confusion because of the overlap between browser oriented code and Node.js oriented code.
150+
const webSocketStream = Ws.createWebSocketStream(socket, options.wsOptions)
153151

154152
webSocketStream['url'] = url
155153
socket.on('close', () => {

src/lib/shared.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Packet, ISubackPacket } from 'mqtt-packet'
2-
import type { Duplex } from 'stream'
2+
import type { Duplex as NativeDuplex } from 'node:stream'
3+
import type { Duplex } from 'readable-stream'
34
import type MqttClient from './client'
45
import type { IClientOptions } from './client'
56

@@ -9,7 +10,7 @@ export type GenericCallback<T> = (error?: Error, result?: T) => void
910

1011
export type VoidCallback = () => void
1112

12-
export type IStream = Duplex & {
13+
export type IStream = (Duplex | NativeDuplex) & {
1314
/** only set on browsers, it's a [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) */
1415
socket?: any
1516
}

tsconfig.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "@tsconfig/node20/tsconfig.json",
24
"compilerOptions": {
3-
"module": "commonjs",
45
"declaration": true,
6+
"lib": ["es2023", "DOM"],
57
"removeComments": true,
68
"emitDecoratorMetadata": true,
79
"experimentalDecorators": true,
810
"allowSyntheticDefaultImports": true,
9-
"target": "es2017",
1011
"sourceMap": true,
1112
"outDir": "./build",
1213
"baseUrl": "./",
1314
"incremental": true,
14-
"skipLibCheck": true,
1515
"preserveSymlinks": true,
16-
"esModuleInterop": true,
1716
"resolveJsonModule": true,
18-
"typeRoots": [
19-
"node_modules/@types"
20-
],
21-
"types": [
22-
"node",
23-
],
17+
"strict": false,
18+
"strictBindCallApply": false,
19+
"strictBuiltinIteratorReturn": false,
20+
"strictFunctionTypes": false,
21+
"strictNullChecks": false,
22+
"strictPropertyInitialization": false,
23+
"alwaysStrict": false,
24+
"noImplicitAny": false,
25+
"noImplicitThis": false,
26+
"useUnknownInCatchVariables": false
2427
},
2528
"include": [
2629
"src",

0 commit comments

Comments
 (0)