Skip to content

Commit

Permalink
chore: update lru-cache dependency, update Node versions in CI (#374)
Browse files Browse the repository at this point in the history
* chore: update lru-cache dependency

* ci: upgrade node versions to 18 and 20

* chore(package.json): set minimum required node version to 18
  • Loading branch information
JKRhb committed Nov 21, 2023
1 parent c8521c9 commit 289f852
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [18.x]
os: [ubuntu-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 14 additions & 13 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -77,8 +77,8 @@ function allAddresses (type): string[] {
return addresses
}

class CoapLRUCache<K, V> extends LRUCache<K, V> {
pruneTimer: NodeJS.Timer
class CoapLRUCache<K extends {}, V extends {}> extends LRUCache<K, V> {
pruneTimer: NodeJS.Timeout
}

class CoAPServer extends EventEmitter {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Expand Down

0 comments on commit 289f852

Please sign in to comment.