Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: use ffi-rs instead to support node 20.11+ versions #314

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ jobs:
- macos-latest
- windows-latest
node: # https://nodejs.org/en/about/releases/
- '*' # Current
- '18' # Active LTS
- '*'
- '20'
- '18'

fail-fast: false

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ jobs:
- macos-latest
- windows-latest
node: # https://nodejs.org/en/about/releases/
- '*' # Current
- '18' # Active LTS
- '*'
- '20'
- '18'
fail-fast: false

steps:
Expand Down
51 changes: 35 additions & 16 deletions lib/ffi-bindings.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,75 @@
const { platform } = require('os')
const { errnoException } = require('./commons')
const Ref = require('ref-napi')
const FFI = require('ffi-napi')
const { load, DataType, open, close, arrayConstructor } = require('ffi-rs')

const LIBRARY_NAME = 'libnative'

const createFFI = () => {
const cInt = Ref.types.int
const cVoid = Ref.types.void
const cInt = DataType.I32
const cVoidRef = DataType.External

return FFI.Library(null, {
//name ret 1 2 3 4 5
setsockopt: [cInt, [cInt, cInt, cInt, Ref.refType(cVoid), cInt]],
getsockopt: [
cInt,
[cInt, cInt, cInt, Ref.refType(cVoid), Ref.refType(cInt)],
],
})
return {
setsockopt: (fd, level, name, value, valueLength) => {
const ret = load({
library: LIBRARY_NAME,
funcName: 'setsockopt',
retType: cInt,
paramsType: [cInt, cInt, cInt, cVoidRef, cInt],
paramsValue: [fd, level, name, value, valueLength],
})
return ret
},
getsockopt: (fd, level, name, value, valueLength) => {
const ret = load({
library: LIBRARY_NAME,
funcName: 'getsockopt',
retType: cInt,
paramsType: [cInt, cInt, cInt, cVoidRef, cVoidRef],
paramsValue: [fd, level, name, value, valueLength],
})
return ret
},
}
}

const ffi = (() => {
let instance
return () => {
if (!instance) {
open({
library: LIBRARY_NAME,
path: '',
})
instance = createFFI()
}
return instance
}
})()

const setsockopt = (fd, level, name, value, valueLength) => {
if(fd == null) {
if (fd == null) {
return false
}

const err = ffi().setsockopt(fd, level, name, value, valueLength)

if (err !== 0) {
const errno = FFI.errno()
const errno = 9 // FIXME: there's no FFI.errno() in ffi-rs
throw errnoException(errno, 'setsockopt')
}

return true
}

const getsockopt = (fd, level, name, value, valueLength) => {
if(fd == null) {
if (fd == null) {
return false
}

const err = ffi().getsockopt(fd, level, name, value, valueLength)

if (err !== 0) {
const errno = FFI.errno()
const errno = 9 // FIXME: there's no FFI.errno() in ffi-rs
throw errnoException(errno, 'getsockopt')
}
return true
Expand Down
82 changes: 65 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* The Missing (TCP_KEEPINTVL and TCP_KEEPCNT) SO_KEEPALIVE socket option setters and getters for Node using
* ffi-napi module.
* ffi-rs module.
*
* Note: For methods provided by this module to work you must enable SO_KEEPALIVE and set the TCP_KEEPIDLE options for
* socket using Net.Socket-s built in method socket.setKeepAlive([enable][, initialDelay]) !
Expand All @@ -22,7 +22,7 @@ const Assert = require('assert'),
Net = require('net'),
OS = require('os'),
Constants = require('./constants'),
Ref = require('ref-napi'),
{ DataType, createPointer, restorePointer } = require('ffi-rs'),
FFIBindings = require('./ffi-bindings')

const _isSocket = (socket) => socket instanceof Net.Socket
Expand Down Expand Up @@ -75,8 +75,12 @@ module.exports.setKeepAliveInterval = function setKeepAliveInterval(

const fd = _getSocketFD(socket),
seconds = ~~(msecs / 1000),
intvlVal = Ref.alloc('int', seconds),
intvlValLn = intvlVal.type.size
dataType = DataType.I32,
[intvlVal] = createPointer({
paramsType: [dataType],
paramsValue: [seconds],
}),
intvlValLn = 4 // sizeof int

return FFIBindings.setsockopt(
fd,
Expand Down Expand Up @@ -114,8 +118,15 @@ module.exports.getKeepAliveInterval = function getKeepAliveInterval(socket) {
)

const fd = _getSocketFD(socket),
intvlVal = Ref.alloc(Ref.types.uint32),
intvlValLn = Ref.alloc(Ref.types.uint32, intvlVal.type.size)
dataType = DataType.I32,
[intvlVal] = createPointer({
paramsType: [dataType],
paramsValue: [0],
}),
[intvlValLn] = createPointer({
paramsType: [DataType.I32],
paramsValue: [4], // sizeof int
})

FFIBindings.getsockopt(
fd,
Expand All @@ -125,7 +136,12 @@ module.exports.getKeepAliveInterval = function getKeepAliveInterval(socket) {
intvlValLn
)

return intvlVal.deref() * 1000
const [value] = restorePointer({
retType: [dataType],
paramsValue: [intvlVal],
})

return value * 1000
}

/**
Expand Down Expand Up @@ -164,8 +180,12 @@ module.exports.setKeepAliveProbes = function setKeepAliveProbes(socket, cnt) {

const fd = _getSocketFD(socket),
count = ~~cnt,
cntVal = Ref.alloc('int', count),
cntValLn = cntVal.type.size
dataType = DataType.I32,
[cntVal] = createPointer({
paramsType: [dataType],
paramsValue: [count],
}),
cntValLn = 4 // sizeof int

return FFIBindings.setsockopt(
fd,
Expand Down Expand Up @@ -203,8 +223,15 @@ module.exports.getKeepAliveProbes = function getKeepAliveProbes(socket) {
)

const fd = _getSocketFD(socket),
cntVal = Ref.alloc(Ref.types.int),
cntValLn = Ref.alloc(Ref.types.int, cntVal.type.size)
dataType = DataType.I32,
[cntVal] = createPointer({
paramsType: [dataType],
paramsValue: [0],
}),
[cntValLn] = createPointer({
paramsType: [DataType.I32],
paramsValue: [4], // sizeof int
})

FFIBindings.getsockopt(
fd,
Expand All @@ -214,7 +241,12 @@ module.exports.getKeepAliveProbes = function getKeepAliveProbes(socket) {
cntValLn
)

return cntVal.deref()
const [value] = restorePointer({
retType: [dataType],
paramsValue: [cntVal],
})

return value
}

/**
Expand Down Expand Up @@ -264,8 +296,12 @@ module.exports.setUserTimeout = function setUserTimeout(socket, msecs) {

const fd = _getSocketFD(socket),
msecInt = ~~msecs,
msecVal = Ref.alloc('int', msecInt),
msecValLn = msecVal.type.size
dataType = DataType.I32,
[msecVal] = createPointer({
paramsType: [dataType],
paramsValue: [msecInt],
}),
msecValLn = 4 // sizeof int

return FFIBindings.setsockopt(
fd,
Expand Down Expand Up @@ -310,8 +346,15 @@ module.exports.getUserTimeout = function getUserTimeout(socket) {
)

const fd = _getSocketFD(socket),
msecVal = Ref.alloc(Ref.types.uint32),
msecValLn = Ref.alloc(Ref.types.uint32, msecVal.type.size)
dataType = DataType.I32,
[msecVal] = createPointer({
paramsType: [dataType],
paramsValue: [0],
}),
[msecValLn] = createPointer({
paramsType: [DataType.I32],
paramsValue: [4], // sizeof int
})

FFIBindings.getsockopt(
fd,
Expand All @@ -321,5 +364,10 @@ module.exports.getUserTimeout = function getUserTimeout(socket) {
msecValLn
)

return msecVal.deref()
const [value] = restorePointer({
retType: [dataType],
paramsValue: [msecVal],
})

return value
}
Loading
Loading