Skip to content

Commit 8a25c30

Browse files
authored
Merge pull request #391 from Eximius/master
2 parents 4a9ce15 + cac1381 commit 8a25c30

File tree

7 files changed

+49
-53
lines changed

7 files changed

+49
-53
lines changed

script/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
set -e
33

44
ZMQ_VERSION=${ZMQ_VERSION:-"4.3.4"}

script/ci/alpine-chroot-install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# https://github.com/alpinelinux/alpine-chroot-install/blob/master/alpine-chroot-install
33
# vim: set ts=4:
44
#---help---
@@ -389,4 +389,4 @@ cat >&2 <<-EOF
389389
---
390390
Alpine installation is complete
391391
Run $CHROOT_DIR/enter-chroot [-u <user>] [command] to enter the chroot.
392-
EOF
392+
EOF

script/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
if [ -z "$CI" ]; then
33
if command -v clang-format >/dev/null; then
44
echo "Clang-format..."

src/draft.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {methods, Socket, SocketType} from "./native"
1+
import {Socket, SocketType} from "./native"
22

33
import {Message, MessageLike, Readable, SocketOptions, Writable} from "."
4-
5-
const {send, receive, join, leave} = methods
4+
import {allowMethods} from "./util"
65

76
export class Server extends Socket {
87
constructor(options?: SocketOptions<Server>) {
@@ -17,7 +16,7 @@ interface ServerRoutingOptions {
1716
export interface Server
1817
extends Readable<[Message, ServerRoutingOptions]>,
1918
Writable<MessageLike, [ServerRoutingOptions]> {}
20-
Object.assign(Server.prototype, {send, receive})
19+
allowMethods(Server.prototype, ["send", "receive"])
2120

2221
export class Client extends Socket {
2322
constructor(options?: SocketOptions<Client>) {
@@ -26,7 +25,7 @@ export class Client extends Socket {
2625
}
2726

2827
export interface Client extends Readable<[Message]>, Writable<MessageLike> {}
29-
Object.assign(Client.prototype, {send, receive})
28+
allowMethods(Client.prototype, ["send", "receive"])
3029

3130
export class Radio extends Socket {
3231
constructor(options?: SocketOptions<Radio>) {
@@ -40,7 +39,10 @@ interface RadioGroupOptions {
4039

4140
// eslint-disable-next-line @typescript-eslint/no-empty-interface
4241
export interface Radio extends Writable<MessageLike, [RadioGroupOptions]> {}
43-
Object.assign(Radio.prototype, {send})
42+
allowMethods(Radio.prototype, ["send"])
43+
44+
const join = (Socket.prototype as any).join
45+
const leave = (Socket.prototype as any).leave
4446

4547
export class Dish extends Socket {
4648
constructor(options?: SocketOptions<Dish>) {
@@ -52,13 +54,13 @@ export class Dish extends Socket {
5254

5355
join(...values: Array<Buffer | string>): void {
5456
for (const value of values) {
55-
join.call(this, value)
57+
join(value)
5658
}
5759
}
5860

5961
leave(...values: Array<Buffer | string>): void {
6062
for (const value of values) {
61-
leave.call(this, value)
63+
leave(value)
6264
}
6365
}
6466
}
@@ -69,7 +71,7 @@ interface DishGroupOptions {
6971

7072
// eslint-disable-next-line @typescript-eslint/no-empty-interface
7173
export interface Dish extends Readable<[Message, DishGroupOptions]> {}
72-
Object.assign(Dish.prototype, {receive})
74+
allowMethods(Dish.prototype, ["receive", "join", "leave"])
7375

7476
export class Gather extends Socket {
7577
constructor(options?: SocketOptions<Gather>) {
@@ -81,7 +83,7 @@ export interface Gather extends Readable<[Message]> {
8183
conflate: boolean
8284
}
8385

84-
Object.assign(Gather.prototype, {receive})
86+
allowMethods(Gather.prototype, ["receive"])
8587

8688
export class Scatter extends Socket {
8789
constructor(options?: SocketOptions<Scatter>) {
@@ -93,7 +95,7 @@ export interface Scatter extends Writable<MessageLike> {
9395
conflate: boolean
9496
}
9597

96-
Object.assign(Scatter.prototype, {send})
98+
allowMethods(Scatter.prototype, ["send"])
9799

98100
export class Datagram extends Socket {
99101
constructor(options?: SocketOptions<Datagram>) {
@@ -104,4 +106,4 @@ export class Datagram extends Socket {
104106
export interface Datagram
105107
extends Readable<[Message, Message]>,
106108
Writable<[MessageLike, MessageLike]> {}
107-
Object.assign(Datagram.prototype, {send, receive})
109+
allowMethods(Datagram.prototype, ["send", "receive"])

src/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {allowMethods} from "./util"
2+
13
export {
24
capability,
35
context,
@@ -14,7 +16,6 @@ export {
1416

1517
import {
1618
capability,
17-
methods,
1819
Context,
1920
EventOfType,
2021
EventType,
@@ -29,8 +30,6 @@ import {
2930
import * as draft from "./draft"
3031
import {FullError} from "./errors"
3132

32-
const {send, receive} = methods
33-
3433
/**
3534
* A type representing the messages that are returned inside promises by
3635
* {@link Readable.receive}().
@@ -976,7 +975,7 @@ export class Pair extends Socket {
976975
}
977976

978977
export interface Pair extends Writable, Readable {}
979-
Object.assign(Pair.prototype, {send, receive})
978+
allowMethods(Pair.prototype, ["send", "receive"])
980979

981980
/**
982981
* A {@link Publisher} socket is used to distribute data to {@link Subscriber}s.
@@ -1028,7 +1027,7 @@ export class Publisher extends Socket {
10281027

10291028
// eslint-disable-next-line @typescript-eslint/no-empty-interface
10301029
export interface Publisher extends Writable {}
1031-
Object.assign(Publisher.prototype, {send})
1030+
allowMethods(Publisher.prototype, ["send"])
10321031

10331032
/**
10341033
* A {@link Subscriber} socket is used to subscribe to data distributed by a
@@ -1128,7 +1127,7 @@ export class Subscriber extends Socket {
11281127

11291128
// eslint-disable-next-line @typescript-eslint/no-empty-interface
11301129
export interface Subscriber extends Readable {}
1131-
Object.assign(Subscriber.prototype, {receive})
1130+
allowMethods(Subscriber.prototype, ["receive"])
11321131

11331132
/**
11341133
* A {@link Request} socket acts as a client to send requests to and receive
@@ -1198,7 +1197,7 @@ export class Request extends Socket {
11981197
}
11991198

12001199
export interface Request extends Readable, Writable {}
1201-
Object.assign(Request.prototype, {send, receive})
1200+
allowMethods(Request.prototype, ["send", "receive"])
12021201

12031202
/**
12041203
* A {@link Reply} socket can act as a server which receives requests from and
@@ -1223,7 +1222,7 @@ export class Reply extends Socket {
12231222
}
12241223

12251224
export interface Reply extends Readable, Writable {}
1226-
Object.assign(Reply.prototype, {send, receive})
1225+
allowMethods(Reply.prototype, ["send", "receive"])
12271226

12281227
/**
12291228
* A {@link Dealer} socket can be used to extend request/reply sockets. Each
@@ -1280,7 +1279,7 @@ export class Dealer extends Socket {
12801279
}
12811280

12821281
export interface Dealer extends Readable, Writable {}
1283-
Object.assign(Dealer.prototype, {send, receive})
1282+
allowMethods(Dealer.prototype, ["send", "receive"])
12841283

12851284
/**
12861285
* A {@link Router} can be used to extend request/reply sockets. When receiving
@@ -1380,7 +1379,7 @@ interface RouterConnectOptions {
13801379
}
13811380

13821381
export interface Router extends Readable, Writable {}
1383-
Object.assign(Router.prototype, {send, receive})
1382+
allowMethods(Router.prototype, ["send", "receive"])
13841383

13851384
/**
13861385
* A {@link Pull} socket is used by a pipeline node to receive messages from
@@ -1405,7 +1404,7 @@ export interface Pull extends Readable {
14051404
conflate: boolean
14061405
}
14071406

1408-
Object.assign(Pull.prototype, {receive})
1407+
allowMethods(Pull.prototype, ["receive"])
14091408

14101409
/**
14111410
* A {@link Push} socket is used by a pipeline node to send messages to
@@ -1436,7 +1435,7 @@ export interface Push extends Writable {
14361435
conflate: boolean
14371436
}
14381437

1439-
Object.assign(Push.prototype, {send})
1438+
allowMethods(Push.prototype, ["send"])
14401439

14411440
/**
14421441
* Same as {@link Publisher}, except that you can receive subscriptions from the
@@ -1516,7 +1515,7 @@ export class XPublisher extends Socket {
15161515
}
15171516

15181517
export interface XPublisher extends Readable, Writable {}
1519-
Object.assign(XPublisher.prototype, {send, receive})
1518+
allowMethods(XPublisher.prototype, ["send", "receive"])
15201519

15211520
/**
15221521
* Same as {@link Subscriber}, except that you subscribe by sending subscription
@@ -1532,7 +1531,7 @@ export class XSubscriber extends Socket {
15321531
}
15331532

15341533
export interface XSubscriber extends Readable, Writable {}
1535-
Object.assign(XSubscriber.prototype, {send, receive})
1534+
allowMethods(XSubscriber.prototype, ["send", "receive"])
15361535

15371536
/**
15381537
* A {@link Stream} is used to send and receive TCP data from a non-ØMQ peer
@@ -1590,7 +1589,7 @@ interface StreamConnectOptions {
15901589
export interface Stream
15911590
extends Readable<[Message, Message]>,
15921591
Writable<[MessageLike, MessageLike]> {}
1593-
Object.assign(Stream.prototype, {send, receive})
1592+
allowMethods(Stream.prototype, ["send", "receive"])
15941593

15951594
/* Meta functionality to define new socket/context options. */
15961595
const enum Type {

src/native.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,6 @@
44
const path = require("path")
55
module.exports = require("node-gyp-build")(path.join(__dirname, ".."))
66

7-
/* We are removing public methods from the Socket prototype that do not apply
8-
to all socket types. We will re-assign them to the prototypes of the
9-
relevant sockets later. For send/receive it is important that they are not
10-
wrapped in JS methods later, to ensure best performance. Any changes to
11-
their signatures should be handled in C++ exclusively. */
12-
interface SpecializedMethods {
13-
send: Function
14-
receive: Function
15-
join: Function
16-
leave: Function
17-
}
18-
19-
const sack: Partial<SpecializedMethods> = {}
20-
const target: SpecializedMethods = module.exports.Socket.prototype
21-
for (const key of ["send", "receive", "join", "leave"] as const) {
22-
sack[key] = target[key]
23-
delete target[key]
24-
}
25-
26-
module.exports.methods = sack
27-
export declare const methods: SpecializedMethods
28-
297
/**
308
* The version of the ØMQ library the bindings were built with. Formatted as
319
* `(major).(minor).(patch)`. For example: `"4.3.2"`.

src/util.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// A union type of possible socket method names to leave available from the native Socket.prototype
2+
type SocketMethods = "send" | "receive" | "join" | "leave"
3+
4+
/**
5+
* This function is used to remove the given methods from the given socket_prototype
6+
* to make the relevant socket types have only their relevant methods.
7+
* @param socketPrototype
8+
* @param methods
9+
*/
10+
export function allowMethods(socketPrototype: any, methods: SocketMethods[]) {
11+
const toDelete = ["send", "receive", "join", "leave"] as SocketMethods[]
12+
for (const method of toDelete) {
13+
if (methods.includes(method)) {
14+
delete socketPrototype[method]
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)