diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f7597af6..c835ecac 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - node-version: [14.x] + node-version: [18.x] os: [ubuntu-latest] steps: diff --git a/.github/workflows/npm-test.yml b/.github/workflows/npm-test.yml index 4d3582e5..554ab727 100644 --- a/.github/workflows/npm-test.yml +++ b/.github/workflows/npm-test.yml @@ -18,7 +18,7 @@ jobs: strategy: matrix: - node-version: [14.x, 16.x, 18.x] + node-version: [18.x, 20.x] os: [ubuntu-latest, windows-latest, macos-latest] steps: diff --git a/lib/server.ts b/lib/server.ts index cdb1d1d0..632c2756 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -12,7 +12,7 @@ import { CoapServerOptions, requestListener, CoapPacket, Block, MiddlewareParame import BlockCache from './cache' import OutgoingMessage from './outgoing_message' import { Socket, createSocket, SocketOptions } from 'dgram' -import LRUCache from 'lru-cache' +import { LRUCache } from 'lru-cache' import os from 'os' import IncomingMessage from './incoming_message' import ObserveStream from './observe_write_stream' @@ -77,8 +77,8 @@ function allAddresses (type): string[] { return addresses } -class CoapLRUCache extends LRUCache { - pruneTimer: NodeJS.Timer +class CoapLRUCache extends LRUCache { + pruneTimer: NodeJS.Timeout } class CoAPServer extends EventEmitter { @@ -148,19 +148,19 @@ class CoAPServer extends EventEmitter { // 32 MB / 1280 = 26214 // The max lifetime is roughly 200s per packet. // Which gave us 131 packets/second guarantee - let max = 32768 * 1024 + let maxSize = 32768 * 1024 if (typeof this._options.cacheSize === 'number' && this._options.cacheSize >= 0) { - max = this._options.cacheSize + maxSize = this._options.cacheSize } this._lru = new CoapLRUCache({ - max, - length: (n, key) => { + maxSize, + sizeCalculation: (n, key) => { return n.buffer.byteLength }, - maxAge: parameters.exchangeLifetime * 1000, - dispose: (key, value) => { + ttl: parameters.exchangeLifetime * 1000, + dispose: (value, key) => { if (value.sender != null) { value.sender.reset() } @@ -345,7 +345,7 @@ class CoAPServer extends EventEmitter { if (parameters.pruneTimerPeriod != null) { // Start LRU pruning timer this._lru.pruneTimer = setInterval(() => { - this._lru.prune() + this._lru.purgeStale() }, parameters.pruneTimerPeriod * 1000) if (this._lru.pruneTimer.unref != null) { this._lru.pruneTimer.unref() @@ -368,11 +368,11 @@ class CoAPServer extends EventEmitter { if (this._internal_socket && this._sock instanceof Socket) { this._sock.close() } - this._lru.reset() + this._lru.clear() this._sock = null this.emit('close') } else { - this._lru.reset() + this._lru.clear() } this._block2Cache.reset() @@ -409,7 +409,8 @@ class CoAPServer extends EventEmitter { if (cached.response != null && (packet.reset ?? false)) { cached.response.end() } - return lru.del(this._toKey(request, packet, false)) + lru.delete(this._toKey(request, packet, false)) + return } else if (packet.ack ?? packet.reset ?? false) { return // nothing to do, ignoring silently } diff --git a/package.json b/package.json index 3d87df14..ddfb3610 100644 --- a/package.json +++ b/package.json @@ -54,18 +54,18 @@ "typescript": "^4.9.3" }, "dependencies": { - "@types/lru-cache": "^5.1.1", + "@types/lru-cache": "^7.10.10", "bl": "^6.0.0", "@types/readable-stream": "^2.3.15", "capitalize": "^2.0.4", "coap-packet": "^1.1.1", "debug": "^4.3.4", "fastseries": "^2.0.0", - "lru-cache": "^6.0.0", + "lru-cache": "^10.0.3", "readable-stream": "^4.2.0" }, "engines": { - "node": ">=12" + "node": ">=18" }, "files": [ "dist/index.d.ts", diff --git a/test/server.ts b/test/server.ts index cae442c4..e5f269a3 100644 --- a/test/server.ts +++ b/test/server.ts @@ -1222,14 +1222,14 @@ describe('server LRU', function () { server.on('request', (req, res) => { res.end() - expect(server._lru.itemCount).to.be.equal(1) + expect(server._lru.size).to.be.equal(1) clock.tick(parameters.exchangeLifetime * 500) - expect(server._lru.itemCount).to.be.equal(1) + expect(server._lru.size).to.be.equal(1) clock.tick(parameters.exchangeLifetime * 1000) - expect(server._lru.itemCount).to.be.equal(0) + expect(server._lru.size).to.be.equal(0) done() }) })