Skip to content

Commit

Permalink
feat: add quic support
Browse files Browse the repository at this point in the history
Ref: #1298
Fixes #1924
  • Loading branch information
robertsLando committed Aug 8, 2024
1 parent 661c30a commit d1a1653
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
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',

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

View workflow job for this annotation

GitHub Actions / browser (20.x)

Property 'alpn' does not exist on type 'IClientOptions'.

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Property 'alpn' does not exist on type 'IClientOptions'.

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

View workflow job for this annotation

GitHub Actions / browser (20.x)

Property 'alpn' does not exist on type 'IClientOptions'.

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

View workflow job for this annotation

GitHub Actions / build (20.x)

Property 'alpn' does not exist on type 'IClientOptions'.
},
})

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

0 comments on commit d1a1653

Please sign in to comment.