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: add quic support #1925

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ export interface ISecureClientOptions {
caPaths?: string | string[]

rejectUnauthorized?: boolean

/**
* optional alpn's
* optional alpn's (Application Layer Protocol Negotiation)
*/
ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array

/**
* optional alpn's (Application Layer Protocol Negotiation)
*/
alpn?: string
}

export type AckHandler = (
Expand Down
8 changes: 7 additions & 1 deletion src/lib/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ function connect(

if (opts.cert && opts.key) {
if (opts.protocol) {
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
if (
['mqtts', 'wss', 'wxs', 'alis', 'quic'].indexOf(
opts.protocol,
) === -1
) {
switch (opts.protocol) {
case 'mqtt':
opts.protocol = 'mqtts'
Expand Down Expand Up @@ -147,6 +151,8 @@ function connect(
protocols.ssl = require('./tls').default
protocols.tls = protocols.ssl
protocols.mqtts = require('./tls').default

protocols.quic = require('./quic').default
} else {
protocols.ws = require('./ws').browserStreamBuilder
protocols.wss = require('./ws').browserStreamBuilder
Expand Down
61 changes: 61 additions & 0 deletions src/lib/connect/quic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { StreamBuilder } from '../shared'
import _debug from 'debug'
import { createSocket } from 'node:quic'

Check failure on line 3 in src/lib/connect/quic.ts

View workflow job for this annotation

GitHub Actions / browser (20.x)

Cannot find module 'node:quic' or its corresponding type declarations.

Check failure on line 3 in src/lib/connect/quic.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'node:quic' or its corresponding type declarations.

Check failure on line 3 in src/lib/connect/quic.ts

View workflow job for this annotation

GitHub Actions / browser (20.x)

Cannot find module 'node:quic' or its corresponding type declarations.

Check failure on line 3 in src/lib/connect/quic.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'node:quic' or its corresponding type declarations.

const debug = _debug('mqttjs:quic')

const buildStream: StreamBuilder = (client, opts) => {
opts.port = opts.port || 8885
opts.host = opts.hostname || opts.host || 'localhost'
opts.servername = opts.host

opts.rejectUnauthorized = opts.rejectUnauthorized !== false

delete opts.path

debug(
'port %d host %s rejectUnauthorized %b',
opts.port,
opts.host,
opts.rejectUnauthorized,
)

const socket = createSocket({
client: {
alpn: opts.alpn || 'mqtt',
},
})

const req = socket.connect({
address: opts.host,
port: opts.port || 8885,
cert: opts.cert,
key: opts.key,
idleTimeout: 0, // disable timeout
})

const stream = req.openStream()

req.on('secure', (servername) => {
// servername is checked for any string for authorisation acceptance
if (opts.rejectUnauthorized && !servername) {
req.emit('error', new Error('TLS not authorized'))
} else {
req.removeListener('error', handleTLSErrors)
}
})

function handleTLSErrors(err) {
if (opts.rejectUnauthorized) {
client.emit('error', err)
}

stream.end()
socket.close()
}

req.on('error', handleTLSErrors)
return stream
}

export default buildStream
Loading