Skip to content

Commit

Permalink
chore: address linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 21, 2024
1 parent d688093 commit 9272712
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
9 changes: 5 additions & 4 deletions lib/incoming_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* See the included LICENSE file for more details.
*/

import { CoapMethod, OptionName } from 'coap-packet'
import { AddressInfo } from 'net'
import { Readable, ReadableOptions } from 'readable-stream'
import { CoapPacket, OptionValue } from '../models/models'
import type { CoapMethod, OptionName } from 'coap-packet'
import type { AddressInfo } from 'net'
import { Readable } from 'readable-stream'
import type { ReadableOptions } from 'readable-stream'
import type { CoapPacket, OptionValue } from '../models/models'
import { packetToMessage } from './helpers'

class IncomingMessage extends Readable {
Expand Down
34 changes: 19 additions & 15 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
*/

import { EventEmitter } from 'events'
import { isIPv6, AddressInfo } from 'net'
import { CoapServerOptions, requestListener, CoapPacket, Block, MiddlewareParameters } from '../models/models'
import { isIPv6, type AddressInfo } from 'net'
import { type CoapServerOptions, type requestListener, type CoapPacket, type Block, type MiddlewareParameters } from '../models/models'
import BlockCache from './cache'
import OutgoingMessage from './outgoing_message'
import { Socket, createSocket, SocketOptions } from 'dgram'
import { Socket, createSocket, type SocketOptions } from 'dgram'
import { LRUCache } from 'lru-cache'
import os from 'os'
import IncomingMessage from './incoming_message'
import ObserveStream from './observe_write_stream'
import RetrySend from './retry_send'
import { handleProxyResponse, handleServerRequest, parseRequest, proxyRequest } from './middlewares'
import { parseBlockOption } from './block'
import { generate, NamedOption, Option, ParsedPacket } from 'coap-packet'
import { generate, type NamedOption, type Option, type ParsedPacket } from 'coap-packet'
import { parseBlock2, createBlock2, getOption, isNumeric, isBoolean } from './helpers'
import { parameters } from './parameters'
import series from 'fastseries'
Expand Down Expand Up @@ -80,6 +80,7 @@ function allAddresses (type): string[] {
return addresses
}

// eslint-disable-next-line @typescript-eslint/ban-types
class CoapLRUCache<K extends {}, V extends {}> extends LRUCache<K, V> {
pruneTimer: NodeJS.Timeout
}
Expand All @@ -91,12 +92,14 @@ interface Block2CacheEntry {

class CoAPServer extends EventEmitter {
_options: CoapServerOptions = {}
_proxiedRequests: Map<string, MiddlewareParameters> = new Map()
_proxiedRequests = new Map<string, MiddlewareParameters>()
// eslint-disable-next-line @typescript-eslint/ban-types
_middlewares: Function[]
_multicastAddress: string | null
_multicastInterface: string | null
_lru: CoapLRUCache<string, any>
_series: any
// eslint-disable-next-line @typescript-eslint/ban-types
_block1Cache: BlockCache<Buffer | {}>
_block2Cache: BlockCache<Block2CacheEntry | null>
_sock: Socket | EventEmitter | null
Expand Down Expand Up @@ -143,12 +146,8 @@ class CoAPServer extends EventEmitter {
this._middlewares.push(handleServerRequest)

// Multicast settings
this._multicastAddress = (this._options.multicastAddress != null)
? this._options.multicastAddress
: null
this._multicastInterface = (this._options.multicastInterface != null)
? this._options.multicastInterface
: null
this._multicastAddress = this._options.multicastAddress ?? null
this._multicastInterface = this._options.multicastInterface ?? null

// We use an LRU cache for the responses to avoid
// DDOS problems.
Expand Down Expand Up @@ -205,6 +204,7 @@ class CoAPServer extends EventEmitter {
rsinfo,
server: this
}
// eslint-disable-next-line @typescript-eslint/ban-types
const activeMiddlewares: Function[] = []

for (let i = 0; i < this._middlewares.length; i++) {
Expand Down Expand Up @@ -284,14 +284,15 @@ class CoAPServer extends EventEmitter {
}
} catch (err) {
if (done != null) {
return done(err)
done(err)
return
} else {
throw err
}
}

if (done != null) {
return done()
done()
}
})

Expand Down Expand Up @@ -414,7 +415,8 @@ class CoAPServer extends EventEmitter {
const cached = lru.peek(this._toKey(request, packet, true))

if (cached != null && !(packet.ack ?? false) && !(packet.reset ?? false) && sock instanceof Socket) {
return sock.send(cached, 0, cached.length, rsinfo.port, rsinfo.address)
sock.send(cached, 0, cached.length, rsinfo.port, rsinfo.address)
return
} else if (cached != null && ((packet.ack ?? false) || (packet.reset ?? false))) {
if (cached.response != null && (packet.reset ?? false)) {
cached.response.end()
Expand All @@ -438,12 +440,13 @@ class CoAPServer extends EventEmitter {
}

if (packet.code === '0.05' && request.headers['Content-Format'] == null) {
return this._sendError(
this._sendError(
Buffer.from('FETCH requests must contain a Content-Format option'),
rsinfo,
undefined,
'4.15' /* TODO: Check if this is the correct error code */
)
return
}

const cacheKey = this._toCacheKey(request, packet)
Expand Down Expand Up @@ -671,6 +674,7 @@ to handle cached answer and blockwise (2)
*/
class OutMessage extends OutgoingMessage {
_cachekey: string
// eslint-disable-next-line @typescript-eslint/ban-types
_addCacheEntry: Function

/**
Expand Down
10 changes: 6 additions & 4 deletions test/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import { nextPort } from './common'
import { expect } from 'chai'
import { parse, generate } from 'coap-packet'
import { request, createServer, Server } from '../index'
import { request, createServer } from '../index'
import type { Server } from '../index'
import dgram from 'dgram'
import tk from 'timekeeper'
import sinon from 'sinon'
import OutgoingMessage from '../lib/outgoing_message'
import IncomingMessage from '../lib/incoming_message'
import type OutgoingMessage from '../lib/outgoing_message'
import type IncomingMessage from '../lib/incoming_message'

describe('proxy', function () {
let server: Server,
Expand Down Expand Up @@ -213,7 +214,8 @@ describe('proxy', function () {
expect(res.code).to.eql('5.00')
expect(res.payload.toString()).to.match(/ENOTFOUND|EAI_AGAIN/)
} catch (err) {
return done(err)
done(err)
return
}
done()
})
Expand Down
14 changes: 9 additions & 5 deletions test/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

import { nextPort } from './common'
import { assert, expect } from 'chai'
import { request, createServer, Server, globalAgent } from '../index'
import { request, createServer, globalAgent } from '../index'
import type { Server } from '../index'
import { toBinary } from '../lib/option_converter'
import { parse, generate } from 'coap-packet'
import { createSocket, Socket } from 'dgram'
import { useFakeTimers } from 'sinon'
import BufferListStream from 'bl'
import OutgoingMessage from '../lib/outgoing_message'
import { AddressInfo } from 'net'
import type OutgoingMessage from '../lib/outgoing_message'
import type { AddressInfo } from 'net'

const originalSetImmediate = setImmediate

describe('request', function () {
Expand Down Expand Up @@ -1425,7 +1427,8 @@ describe('request', function () {
expect(packet.options[0].name).to.eql('Observe')
expect(packet.options[0].value).to.eql(Buffer.from([1]))
} catch (err) {
return done(err)
done(err)
return
}
done()
})
Expand Down Expand Up @@ -1467,7 +1470,8 @@ describe('request', function () {
expect(packet.options[0].name).to.eql('Observe')
expect(packet.options[0].value).to.eql(Buffer.from([1]))
} catch (err) {
return done(err)
done(err)
return
}

done()
Expand Down
9 changes: 5 additions & 4 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import { parse, generate } from 'coap-packet'
import { nextPort } from './common'
import { expect } from 'chai'
import { CoapPacket, CoapServerOptions, Option } from '../models/models'
import type { CoapPacket, CoapServerOptions, Option } from '../models/models'
import { request, createServer } from '../index'
import { createSocket } from 'dgram'
import { type Socket, createSocket } from 'dgram'
import BufferListStream = require('bl')
import tk from 'timekeeper'
import sinon from 'sinon'
import { EventEmitter } from 'events'
import { parameters } from '../lib/parameters'
import IncomingMessage from '../lib/incoming_message'
import type IncomingMessage from '../lib/incoming_message'

const originalSetImmediate = setImmediate

Expand Down Expand Up @@ -703,7 +703,8 @@ describe('server', function () {
// original one plus 4 retries
expect(messages).to.eql(5)
} catch (err) {
return done(err)
done(err)
return
}
done()
}, 45 * 1000)
Expand Down
9 changes: 5 additions & 4 deletions test/share-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import { nextPort } from './common'
import { expect } from 'chai'
import { Agent, Server, request, createServer, setGlobalAgent, globalAgent } from '../index'
import IncomingMessage from '../lib/incoming_message'
import OutgoingMessage from '../lib/outgoing_message'
import { AddressInfo } from 'net'
import { Agent, request, createServer, setGlobalAgent, globalAgent } from '../index'
import type { Server } from '../index'
import type IncomingMessage from '../lib/incoming_message'
import type OutgoingMessage from '../lib/outgoing_message'
import type { AddressInfo } from 'net'
import sinon = require('sinon')
import { Socket } from 'dgram'

Expand Down

0 comments on commit 9272712

Please sign in to comment.