Skip to content

Commit

Permalink
feat(win32)!: allow install and just no-op unsupported operations (#288)
Browse files Browse the repository at this point in the history
* feat: allow windows installs

all the methods are no-op on windows always returning false

* feat: run actions on windows

* chore: update docs

* feat!: guard against nullish descriptors
  • Loading branch information
hertzg committed Aug 11, 2021
1 parent e290671 commit 6a6f9b8
Show file tree
Hide file tree
Showing 11 changed files with 12,004 additions and 68 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
os:
- ubuntu-latest
- macos-latest
- windows-latest
node: # https://nodejs.org/en/about/releases/
- '*' # Current
- '14' # Active LTS
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
os:
- ubuntu-latest
- macos-latest
- windows-latest
node: # https://nodejs.org/en/about/releases/
- '*' # Current
- '14' # Active LTS
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

The Missing (`TCP_KEEPINTVL` and `TCP_KEEPCNT`) `SO_KEEPALIVE` socket option setters and getters for Node using [`ffi-napi`](https://www.npmjs.com/package/ffi-napi) module.

Tested on 🐧 `linux` & 🍏 `osx` (both `amd64` and `arm64`), should work on 😈 `freebsd` and others. Does not work on 🐄 `win32` (pull requests welcome).
Tested on 🐧 `linux` & 🍏 `osx` (both `amd64` and `arm64`), should work on 😈 `freebsd` and others.
Installs on 🐄 `win32` 🎉 but methods are no-ops (pull requests welcome).

There's also support for getting & setting the `TCP_USER_TIMEOUT` (🐧 `linux` and 🍏 `osx` only) option, which is closely related to keep-alive.

Expand All @@ -44,11 +45,12 @@ There's also support for getting & setting the `TCP_USER_TIMEOUT` (🐧 `linux`
| 🐧 `linux` ||||
| 🍏 `osx` ||| ✅ (`TCP_RXT_CONNDROPTIME`) |
| 😈 `freebsd` ||||
| 🐄 `win32` | | | |
| 🐄 `win32` | | | |

Legend:

- ✅ - Supported
- ➖ - No operation
- ❌ - Unsupported (throws)

## Install
Expand Down
63 changes: 44 additions & 19 deletions lib/ffi-bindings.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
'use strict'
const { platform } = require('os')
const { errnoException } = require('./commons')
const Ref = require('ref-napi')
const FFI = require('ffi-napi')

const Ref = require('ref-napi'),
FFI = require('ffi-napi'),
Commons = require('./commons')
const createFFI = () => {
const cInt = Ref.types.int
const cVoid = Ref.types.void

const cInt = Ref.types.int,
cVoid = Ref.types.void
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)],
],
})
}

const ffi = 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)]],
})
const ffi = (() => {
let instance
return () => {
if (!instance) {
instance = createFFI()
}
return instance
}
})()

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

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

if (err !== 0) {
let errno = FFI.errno()
throw Commons.errnoException(errno, 'setsockopt')
const errno = FFI.errno()
throw errnoException(errno, 'setsockopt')
}

return true
}

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

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

if (err !== 0) {
let errno = FFI.errno()
throw Commons.errnoException(errno, 'getsockopt')
const errno = FFI.errno()
throw errnoException(errno, 'getsockopt')
}
return true
}

const noop = () => false
const isWin32 = platform() === 'win32'

module.exports = {
setsockopt,
getsockopt,
setsockopt: isWin32 ? noop : setsockopt,
getsockopt: isWin32 ? noop : getsockopt,
}
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

const Assert = require('assert'),
Net = require('net'),
OS = require('os'),
Constants = require('./constants'),
Ref = require('ref-napi'),
FFIBindings = require('./ffi-bindings')
Expand All @@ -28,7 +29,9 @@ const _isSocket = (socket) => socket instanceof Net.Socket

const _getSocketFD = (socket) => {
const fd = socket._handle != null ? socket._handle.fd : undefined
Assert(fd && fd !== -1, 'Unable to get socket fd')
if (OS.platform() !== 'win32') {
Assert(fd && fd !== -1, 'Unable to get socket fd')
}
return fd
}

Expand Down
Loading

0 comments on commit 6a6f9b8

Please sign in to comment.