From 4d796a68efdd2083ce694f4fbb702d9902b1c3d2 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Fri, 21 Jun 2024 16:35:41 +0200 Subject: [PATCH] chore: address linting issues --- lib/incoming_message.ts | 9 +++++---- lib/server.ts | 34 +++++++++++++++++++--------------- test/proxy.ts | 10 ++++++---- test/request.ts | 14 +++++++++----- test/server.ts | 9 +++++---- test/share-socket.ts | 9 +++++---- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/lib/incoming_message.ts b/lib/incoming_message.ts index 10553ac2..5453f050 100644 --- a/lib/incoming_message.ts +++ b/lib/incoming_message.ts @@ -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 { diff --git a/lib/server.ts b/lib/server.ts index 56d325b7..504ec66a 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -7,11 +7,11 @@ */ 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' @@ -19,7 +19,7 @@ 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' @@ -80,6 +80,7 @@ function allAddresses (type): string[] { return addresses } +// eslint-disable-next-line @typescript-eslint/ban-types class CoapLRUCache extends LRUCache { pruneTimer: NodeJS.Timeout } @@ -91,12 +92,14 @@ interface Block2CacheEntry { class CoAPServer extends EventEmitter { _options: CoapServerOptions = {} - _proxiedRequests: Map = new Map() + _proxiedRequests = new Map() + // eslint-disable-next-line @typescript-eslint/ban-types _middlewares: Function[] _multicastAddress: string | null _multicastInterface: string | null _lru: CoapLRUCache _series: any + // eslint-disable-next-line @typescript-eslint/ban-types _block1Cache: BlockCache _block2Cache: BlockCache _sock: Socket | EventEmitter | null @@ -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. @@ -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++) { @@ -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() } }) @@ -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() @@ -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) @@ -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 /** diff --git a/test/proxy.ts b/test/proxy.ts index 38305415..1389ea42 100644 --- a/test/proxy.ts +++ b/test/proxy.ts @@ -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, @@ -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() }) diff --git a/test/request.ts b/test/request.ts index 6dc1c39e..1a9b94a0 100644 --- a/test/request.ts +++ b/test/request.ts @@ -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 () { @@ -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() }) @@ -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() diff --git a/test/server.ts b/test/server.ts index e5f269a3..783f204c 100644 --- a/test/server.ts +++ b/test/server.ts @@ -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 @@ -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) diff --git a/test/share-socket.ts b/test/share-socket.ts index 69373f3a..15fb02fe 100644 --- a/test/share-socket.ts +++ b/test/share-socket.ts @@ -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'