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: improve compatibility types + convert compat tests to TypeScript #671

Merged
merged 25 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e4b2462
test: fix mocha ts-node failing
aminya Oct 27, 2024
a4d73b9
test: remove timeout/slow calls
aminya Oct 27, 2024
84442ea
fix: export the type of the compat options in TypeScript
aminya Oct 27, 2024
0b42f49
test: run the compat tests as part of test
aminya Oct 27, 2024
858dc2b
test: convert compat export tests to typescript
aminya Oct 27, 2024
18e704a
test: convert context tests to typescript
aminya Oct 27, 2024
4cf789d
test: convert compat gc tests to typescript
aminya Oct 27, 2024
5491633
fix: accept single message in send of compat mode
aminya Oct 27, 2024
125e5ce
test: convert socket error callback to typescript
aminya Oct 27, 2024
fe042d7
test: fix socket error callback compat test
aminya Oct 27, 2024
117fcee
fix: accept numbers in MessageLike
aminya Oct 27, 2024
141778f
test: convert socket event/messages to TypeScript
aminya Oct 27, 2024
c7de157
fix: make the properties of monitor optional in compat
aminya Oct 27, 2024
27601b2
test: convert monitor tests to TypeScript
aminya Oct 27, 2024
c9bfe82
test: convert socket pair. push, req, router compat tests to TypeScript
aminya Oct 27, 2024
d3f16a3
fix: fix linting issues in the compat layer
aminya Oct 27, 2024
b658989
test: convert socket stream compat to TypeScript
aminya Oct 27, 2024
cd82f49
fix: do not mix done/async
aminya Oct 27, 2024
ad9cd4c
test: convert all proxy compat tests to TypeScript
aminya Oct 27, 2024
676b4ce
test: convert rest of the socket compat tests to TypeScript
aminya Oct 27, 2024
93efd26
test: fix the curve capability check in compat
aminya Oct 27, 2024
8c06d7e
test: avoid blockage when running compat tests
aminya Oct 27, 2024
0009d11
ci: run test and compatibility tests separately in CI
aminya Oct 27, 2024
1489e60
fix: fix the check for compat tests
aminya Oct 28, 2024
40ab894
chore: use installed mocha for vscode
aminya Oct 28, 2024
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@typescript-eslint/quotes": ["error", "double"],
"require-await": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-extraneous-class": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-await-in-loop": "off",
"class-methods-use-this": "off"
Expand Down
14 changes: 13 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,19 @@ jobs:
timeout_minutes: 5
max_attempts: 1
command: |
pnpm run test
pnpm run clean.temp
pnpm run test.unit
continue-on-error: true

- name: Test Compatibility
if: ${{ !matrix.docker }}
uses: nick-fields/retry@v3
with:
timeout_minutes: 5
max_attempts: 1
command: |
pnpm run clean.temp
pnpm run test.unit.compat
continue-on-error: true

- name: Test Electron (Main)
Expand Down
7 changes: 4 additions & 3 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*/
const config = {
require: ["ts-node/register"],
spec: ["test/unit/*-test.ts", "test/unit/compat/*-test.js"],
"expose-gc": true,
"v8-expose-gc": true,
exit: true,
parallel: true,
timeout: 10000,
retries: 3,
timeout: 5000,
retries: 1,
fullTrace: true,
bail: false,
}

module.exports = config
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
},
"mochaExplorer.parallel": true,
"mochaExplorer.globImplementation": "vscode",
"mochaExplorer.nodeArgv": [
"--expose-gc"
],
"mochaExplorer.nodeArgv": ["--expose-gc"],
"mochaExplorer.debuggerConfig": "JS-Attach",
"mochaExplorer.env": {
"INCLUDE_COMPAT_TESTS": "true"
},
"mochaExplorer.files": "test/unit/**/*-test.ts",
"mochaExplorer.mochaPath": "./node_modules/mocha",
"files.exclude": {
"**/.DS_Store": true,
"**/Thumbs.db": true,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/which": "^3.0.4",
"benchmark": "^2.1.4",
"chai": "^4",
"cross-env": "^7.0.3",
"deasync": "^0.1.30",
"downlevel-dts": "^0.11.0",
"electron-mocha": "^13.0.0",
Expand Down Expand Up @@ -97,9 +98,11 @@
"build.native.debug": "cmake-ts dev-os-only",
"build": "run-p build.js build.native",
"build.debug": "run-s build.js build.native.debug",
"test": "run-s clean.temp build && mocha",
"test": "run-s test.unit",
"test.unit": "run-s clean.temp build && cross-env INCLUDE_COMPAT_TESTS=false mocha ./test/unit/*-test.ts",
"test.unit.compat": "run-s clean.temp build && cross-env INCLUDE_COMPAT_TESTS=true mocha ./test/unit/compat/*-test.ts",
"test.smoke": "bash ./script/smoke-test.bash",
"test.skip_gc_tests": "run-s clean.temp build.debug && cross-env SKIP_GC_TESTS=true mocha",
"test.skip_gc_tests": "run-s clean.temp build && cross-env SKIP_GC_TESTS=true mocha",
"test.electron.main": "run-s clean.temp build && electron-mocha",
"format": "run-s format.prettier format.clang-format",
"format.prettier": "prettier -l --cache --cache-location ./.cache/prettier --write .",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 30 additions & 115 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import {EventEmitter} from "events"
import * as zmq from "."
import {FullError} from "./errors"
import * as longOptions from "./compat/long-options"
import * as pollStates from "./compat/poll-states"
import * as sendOptions from "./compat/send-options"

type AnySocket =
| zmq.Pair
Expand All @@ -22,99 +25,6 @@ type AnySocket =
| zmq.Stream

let count = 1
const types = {
ZMQ_PAIR: 0,
ZMQ_PUB: 1,
ZMQ_SUB: 2,
ZMQ_REQ: 3,
ZMQ_REP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Y: 50,
ZMQ_ZAP_DOMAIN: 55,
ZMQ_HEARTBEAT_IVL: 75,
ZMQ_HEARTBEAT_TTL: 76,
ZMQ_HEARTBEAT_TIMEOUT: 77,
ZMQ_CONNECT_TIMEOUT: 79,
ZMQ_IO_THREADS: 1,
ZMQ_MAX_SOCKETS: 2,
ZMQ_ROUTER_HANDOVER: 56,
}

const pollStates = {
ZMQ_POLLIN: 1,
ZMQ_POLLOUT: 2,
ZMQ_POLLERR: 4,
}

const sendOptions = {
ZMQ_SNDMORE: 2,
}

const capabilities = {
ZMQ_CAN_MONITOR: 1,
ZMQ_CAN_DISCONNECT: 1,
ZMQ_CAN_UNBIND: 1,
ZMQ_CAN_SET_CTX: 1,
}

const socketStates = {
STATE_READY: 0,
STATE_BUSY: 1,
STATE_CLOSED: 2,
}

const shortOptions = {
_fd: longOptions.ZMQ_FD,
Expand Down Expand Up @@ -241,9 +151,11 @@ class Socket extends EventEmitter {
case "stream":
this._socket = new zmq.Stream()
break
default:
throw new Error(`Invalid socket type: ${type}`)
}

const recv = async () => {
const recv = () => {
this.once("_flushRecv", async () => {
while (!this._socket.closed && !this._paused) {
await this._recv()
Expand Down Expand Up @@ -347,7 +259,7 @@ class Socket extends EventEmitter {
.catch(err => {
process.nextTick(() => {
if (cb) {
cb(err)
cb(err as Error)
} else {
this.emit("error", err)
}
Expand All @@ -371,7 +283,7 @@ class Socket extends EventEmitter {
.catch(err => {
process.nextTick(() => {
if (cb) {
cb(err)
cb(err as Error)
} else {
this.emit("error", err)
}
Expand All @@ -391,8 +303,12 @@ class Socket extends EventEmitter {
return this
}

send(message: zmq.MessageLike[], flags = 0, cb?: Callback) {
flags = flags | 0
send(
message: zmq.MessageLike[] | zmq.MessageLike,
givenFlags: number | undefined | null = 0,
cb: Callback | undefined = undefined,
) {
const flags = (givenFlags ?? 0) | 0
this._msg = this._msg.concat(message)
if ((flags & sendOptions.ZMQ_SNDMORE) === 0) {
this._sendQueue.push([this._msg, cb])
Expand Down Expand Up @@ -460,7 +376,7 @@ class Socket extends EventEmitter {
return this._socket.closed
}

monitor(interval: number, num: number) {
monitor(interval?: number, num?: number) {
this._count = count++

/* eslint-disable-next-line no-unused-expressions */
Expand Down Expand Up @@ -560,8 +476,9 @@ class Socket extends EventEmitter {
}
}

setsockopt(option: number | keyof typeof shortOptions, value: any) {
option = typeof option !== "number" ? shortOptions[option] : option
setsockopt(givenOption: number | keyof typeof shortOptions, value: any) {
const option =
typeof givenOption === "number" ? givenOption : shortOptions[givenOption]

switch (option) {
case longOptions.ZMQ_AFFINITY:
Expand Down Expand Up @@ -699,8 +616,9 @@ class Socket extends EventEmitter {
return this
}

getsockopt(option: number | keyof typeof shortOptions) {
option = typeof option !== "number" ? shortOptions[option] : option
getsockopt(givenOption: number | keyof typeof shortOptions) {
const option =
typeof givenOption !== "number" ? shortOptions[givenOption] : givenOption

switch (option) {
case longOptions.ZMQ_AFFINITY:
Expand Down Expand Up @@ -822,10 +740,9 @@ for (const key in shortOptions) {
get(this: Socket) {
return this.getsockopt(shortOptions[key as keyof typeof shortOptions])
},
set(this: Socket, val: string | Buffer) {
if ("string" === typeof val) {
val = Buffer.from(val, "utf8")
}
set(this: Socket, givenVal: string | Buffer) {
const val =
typeof givenVal === "string" ? Buffer.from(givenVal, "utf8") : givenVal
return this.setsockopt(
shortOptions[key as keyof typeof shortOptions],
val,
Expand Down Expand Up @@ -909,11 +826,9 @@ export {
shortOptions as options,
}

/* Unfortunately there is no easy way to include these in the resulting
TS definitions. */
Object.assign(module.exports, longOptions)
Object.assign(module.exports, types)
Object.assign(module.exports, pollStates)
Object.assign(module.exports, sendOptions)
Object.assign(module.exports, socketStates)
Object.assign(module.exports, capabilities)
export * from "./compat/long-options"
export * from "./compat/types"
export * from "./compat/poll-states"
export * from "./compat/send-options"
export * from "./compat/capabilities"
export * from "./compat/socket-states"
4 changes: 4 additions & 0 deletions src/compat/capabilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ZMQ_CAN_MONITOR = 1
export const ZMQ_CAN_DISCONNECT = 1
export const ZMQ_CAN_UNBIND = 1
export const ZMQ_CAN_SET_CTX = 1
51 changes: 51 additions & 0 deletions src/compat/long-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const ZMQ_AFFINITY = 4
export const ZMQ_IDENTITY = 5
export const ZMQ_SUBSCRIBE = 6
export const ZMQ_UNSUBSCRIBE = 7
export const ZMQ_RATE = 8
export const ZMQ_RECOVERY_IVL = 9
export const ZMQ_RECOVERY_IVL_MSEC = 9
export const ZMQ_SNDBUF = 11
export const ZMQ_RCVBUF = 12
export const ZMQ_RCVMORE = 13
export const ZMQ_FD = 14
export const ZMQ_EVENTS = 15
export const ZMQ_TYPE = 16
export const ZMQ_LINGER = 17
export const ZMQ_RECONNECT_IVL = 18
export const ZMQ_BACKLOG = 19
export const ZMQ_RECONNECT_IVL_MAX = 21
export const ZMQ_MAXMSGSIZE = 22
export const ZMQ_SNDHWM = 23
export const ZMQ_RCVHWM = 24
export const ZMQ_MULTICAST_HOPS = 25
export const ZMQ_RCVTIMEO = 27
export const ZMQ_SNDTIMEO = 28
export const ZMQ_IPV4ONLY = 31
export const ZMQ_LAST_ENDPOINT = 32
export const ZMQ_ROUTER_MANDATORY = 33
export const ZMQ_TCP_KEEPALIVE = 34
export const ZMQ_TCP_KEEPALIVE_CNT = 35
export const ZMQ_TCP_KEEPALIVE_IDLE = 36
export const ZMQ_TCP_KEEPALIVE_INTVL = 37
export const ZMQ_TCP_ACCEPT_FILTER = 38
export const ZMQ_DELAY_ATTACH_ON_CONNECT = 39
export const ZMQ_XPUB_VERBOSE = 40
export const ZMQ_ROUTER_RAW = 41
export const ZMQ_IPV6 = 42
export const ZMQ_MECHANISM = 43
export const ZMQ_PLAIN_SERVER = 44
export const ZMQ_PLAIN_USERNAME = 45
export const ZMQ_PLAIN_PASSWORD = 46
export const ZMQ_CURVE_SERVER = 47
export const ZMQ_CURVE_PUBLICKEY = 48
export const ZMQ_CURVE_SECRETKEY = 49
export const ZMQ_CURVE_SERVERKEY = 50
export const ZMQ_ZAP_DOMAIN = 55
export const ZMQ_HEARTBEAT_IVL = 75
export const ZMQ_HEARTBEAT_TTL = 76
export const ZMQ_HEARTBEAT_TIMEOUT = 77
export const ZMQ_CONNECT_TIMEOUT = 79
export const ZMQ_IO_THREADS = 1
export const ZMQ_MAX_SOCKETS = 2
export const ZMQ_ROUTER_HANDOVER = 56
Loading
Loading