Skip to content

Commit 4bb3dd3

Browse files
chore(release): 5.2.0 [skip ci]
# [5.2.0](v5.1.0...v5.2.0) (2021-03-12) ### Features * Export Socket class ([#104](#104)) ([72db5fa](72db5fa))
1 parent 72db5fa commit 4bb3dd3

File tree

7 files changed

+211
-21
lines changed

7 files changed

+211
-21
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [5.2.0](https://github.com/Rapsssito/react-native-tcp-socket/compare/v5.1.0...v5.2.0) (2021-03-12)
2+
3+
4+
### Features
5+
6+
* Export Socket class ([#104](https://github.com/Rapsssito/react-native-tcp-socket/issues/104)) ([72db5fa](https://github.com/Rapsssito/react-native-tcp-socket/commit/72db5faef44e586c0d9fcc4752f13d17caba46cf))
7+
18
# [5.1.0](https://github.com/Rapsssito/react-native-tcp-socket/compare/v5.0.0...v5.1.0) (2021-03-06)
29

310

coverage/coverage-final.json

+4-4
Large diffs are not rendered by default.

lib/types/Globals.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export const nativeEventEmitter: import("react-native").EventEmitter;
2-
export function getInstanceNumber(): number;
2+
export function getNextId(): number;

lib/types/Server.d.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44
export default class Server extends EventEmitter<"error" | "close" | "connection" | "listening", any> {
55
/**
6-
* @param {(socket: TcpSocket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
6+
* @param {(socket: Socket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
77
*/
8-
constructor(connectionCallback?: ((socket: TcpSocket) => void) | undefined);
8+
constructor(connectionCallback?: ((socket: Socket) => void) | undefined);
99
/** @private */
1010
private _id;
1111
/** @private */
1212
private _eventEmitter;
13-
/** @private @type {TcpSocket[]} */
13+
/** @private @type {Set<Socket>} */
1414
private _connections;
1515
/** @private */
1616
private _localAddress;
@@ -62,31 +62,27 @@ export default class Server extends EventEmitter<"error" | "close" | "connection
6262
* on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
6363
* `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
6464
*
65-
* @returns {import('./TcpSocket').AddressInfo | null}
65+
* @returns {import('./Socket').AddressInfo | null}
6666
*/
67-
address(): import('./TcpSocket').AddressInfo | null;
67+
address(): import('./Socket').AddressInfo | null;
6868
ref(): Server;
6969
unref(): Server;
7070
/**
7171
* @private
7272
*/
7373
private _registerEvents;
7474
_errorListener: import("react-native").EmitterSubscription | undefined;
75-
_closeListener: import("react-native").EmitterSubscription | undefined;
7675
_connectionsListener: import("react-native").EmitterSubscription | undefined;
77-
/**
78-
* @private
79-
*/
80-
private _unregisterEvents;
8176
/**
8277
* @private
8378
*/
8479
private _setDisconnected;
8580
/**
8681
* @private
87-
* @param {{ id: number; connection: import('./TcpSocket').NativeConnectionInfo; }} info
82+
* @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; }} info
83+
* @returns {Socket}
8884
*/
8985
private _buildSocket;
9086
}
9187
import EventEmitter from "eventemitter3";
92-
import TcpSocket from "./TcpSocket";
88+
import Socket from "./Socket";

lib/types/Socket.d.ts

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* @typedef {"ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex"} BufferEncoding
3+
*
4+
* @typedef {import('react-native').NativeEventEmitter} NativeEventEmitter
5+
*
6+
* @typedef {{address: string, family: string, port: number}} AddressInfo
7+
*
8+
* @typedef {{localAddress: string, localPort: number, remoteAddress: string, remotePort: number, remoteFamily: string}} NativeConnectionInfo
9+
*
10+
* @typedef {{
11+
* port: number;
12+
* host?: string;
13+
* timeout?: number,
14+
* localAddress?: string,
15+
* localPort?: number,
16+
* interface?: 'wifi' | 'cellular' | 'ethernet',
17+
* reuseAddress?: boolean,
18+
* tls?: boolean,
19+
* tlsCheckValidity?: boolean,
20+
* tlsCert?: any,
21+
* }} ConnectionOptions
22+
*
23+
* @extends {EventEmitter<'connect' | 'timeout' | 'data' | 'error' | 'close', any>}
24+
*/
25+
export default class Socket extends EventEmitter<"connect" | "timeout" | "data" | "error" | "close", any> {
26+
/** @private */
27+
private _id;
28+
/** @private */
29+
private _eventEmitter;
30+
/** @type {number} @private */
31+
private _timeoutMsecs;
32+
/** @private */
33+
private _timeout;
34+
/** @type {number} @private */
35+
private _state;
36+
/** @private */
37+
private _encoding;
38+
localAddress: string | undefined;
39+
localPort: number | undefined;
40+
remoteAddress: string | undefined;
41+
remotePort: number | undefined;
42+
remoteFamily: string | undefined;
43+
/**
44+
* @package
45+
* @param {number} id
46+
*/
47+
_setId(id: number): void;
48+
/**
49+
* @package
50+
* @param {NativeConnectionInfo} connectionInfo
51+
*/
52+
_setConnected(connectionInfo: NativeConnectionInfo): void;
53+
/**
54+
* @param {ConnectionOptions} options
55+
* @param {() => void} [callback]
56+
*/
57+
connect(options: ConnectionOptions, callback?: (() => void) | undefined): Socket;
58+
_destroyed: boolean | undefined;
59+
/**
60+
* Sets the socket to timeout after `timeout` milliseconds of inactivity on the socket. By default `TcpSocket` do not have a timeout.
61+
*
62+
* When an idle timeout is triggered the socket will receive a `'timeout'` event but the connection will not be severed.
63+
* The user must manually call `socket.end()` or `socket.destroy()` to end the connection.
64+
*
65+
* If `timeout` is 0, then the existing idle timeout is disabled.
66+
*
67+
* The optional `callback` parameter will be added as a one-time listener for the `'timeout'` event.
68+
*
69+
* @param {number} timeout
70+
* @param {() => void} [callback]
71+
*/
72+
setTimeout(timeout: number, callback?: (() => void) | undefined): Socket;
73+
/**
74+
* @private
75+
* @param {number} [timeout]
76+
*/
77+
private _activateTimer;
78+
/**
79+
* @private
80+
*/
81+
private _clearTimeout;
82+
/**
83+
* Set the encoding for the socket as a Readable Stream. By default, no encoding is assigned and stream data will be returned as `Buffer` objects.
84+
* Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects.
85+
*
86+
* For instance, calling `socket.setEncoding('utf8')` will cause the output data to be interpreted as UTF-8 data, and passed as strings.
87+
* Calling `socket.setEncoding('hex')` will cause the data to be encoded in hexadecimal string format.
88+
*
89+
* @param {BufferEncoding} [encoding]
90+
*/
91+
setEncoding(encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined): Socket;
92+
/**
93+
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
94+
*
95+
* Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
96+
*
97+
* Passing `true` for `noDelay` or not passing an argument will disable Nagle's algorithm for the socket. Passing false for noDelay will enable Nagle's algorithm.
98+
*
99+
* @param {boolean} noDelay Default: `true`
100+
*/
101+
setNoDelay(noDelay?: boolean): Socket;
102+
/**
103+
* Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
104+
*
105+
* `initialDelay` is ignored.
106+
*
107+
* @param {boolean} enable Default: `false`
108+
* @param {number} initialDelay ***IGNORED**. Default: `0`
109+
*/
110+
setKeepAlive(enable?: boolean, initialDelay?: number): Socket;
111+
/**
112+
* Returns the bound `address`, the address `family` name and `port` of the socket as reported
113+
* by the operating system: `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
114+
*
115+
* @returns {AddressInfo | {}}
116+
*/
117+
address(): AddressInfo | {};
118+
/**
119+
* @param {string | Buffer | Uint8Array} data
120+
* @param {BufferEncoding} [encoding]
121+
*/
122+
end(data: string | Buffer | Uint8Array, encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined): void;
123+
destroy(): void;
124+
/**
125+
* Sends data on the socket. The second parameter specifies the encoding in the case of a string — it defaults to UTF8 encoding.
126+
*
127+
* The optional callback parameter will be executed when the data is finally written out, which may not be immediately.
128+
*
129+
* @param {string | Buffer | Uint8Array} buffer
130+
* @param {BufferEncoding} [encoding]
131+
* @param {(error: string | null) => void} [callback]
132+
*/
133+
write(buffer: string | Buffer | Uint8Array, encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined, callback?: ((error: string | null) => void) | undefined): void;
134+
ref(): void;
135+
unref(): void;
136+
/**
137+
* @private
138+
*/
139+
private _registerEvents;
140+
_dataListener: import("react-native").EmitterSubscription | undefined;
141+
_errorListener: import("react-native").EmitterSubscription | undefined;
142+
_closeListener: import("react-native").EmitterSubscription | undefined;
143+
_connectListener: import("react-native").EmitterSubscription | undefined;
144+
/**
145+
* @private
146+
*/
147+
private _unregisterEvents;
148+
/**
149+
* @private
150+
* @param {string | Buffer | Uint8Array} buffer
151+
* @param {BufferEncoding} [encoding]
152+
*/
153+
private _generateSendBuffer;
154+
/**
155+
* @private
156+
*/
157+
private _setDisconnected;
158+
}
159+
export type BufferEncoding = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex";
160+
export type NativeEventEmitter = import("react-native").NativeEventEmitter;
161+
export type AddressInfo = {
162+
address: string;
163+
family: string;
164+
port: number;
165+
};
166+
export type NativeConnectionInfo = {
167+
localAddress: string;
168+
localPort: number;
169+
remoteAddress: string;
170+
remotePort: number;
171+
remoteFamily: string;
172+
};
173+
export type ConnectionOptions = {
174+
port: number;
175+
host?: string | undefined;
176+
timeout?: number | undefined;
177+
localAddress?: string | undefined;
178+
localPort?: number | undefined;
179+
interface?: "wifi" | "cellular" | "ethernet" | undefined;
180+
reuseAddress?: boolean | undefined;
181+
tls?: boolean | undefined;
182+
tlsCheckValidity?: boolean | undefined;
183+
tlsCert?: any;
184+
};
185+
import EventEmitter from "eventemitter3";
186+
import { Buffer } from "buffer";

lib/types/index.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare namespace _default {
22
export { createServer };
33
export { createConnection };
44
export { Server };
5+
export { Socket };
56
}
67
export default _default;
78
/**
@@ -10,10 +11,10 @@ export default _default;
1011
*/
1112
declare function createServer(connectionListener: (socket: Socket) => void): Server;
1213
/**
13-
* @param {import('./TcpSocket').ConnectionOptions} options
14+
* @param {import('./Socket').ConnectionOptions} options
1415
* @param {() => void} callback
1516
* @returns {Socket}
1617
*/
17-
declare function createConnection(options: import('./TcpSocket').ConnectionOptions, callback: () => void): Socket;
18+
declare function createConnection(options: import('./Socket').ConnectionOptions, callback: () => void): Socket;
1819
import Server from "./Server";
19-
import Socket from "./TcpSocket";
20+
import Socket from "./Socket";

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-tcp-socket",
33
"title": "React Native Tcp Socket",
4-
"version": "5.1.0",
4+
"version": "5.2.0",
55
"description": "React Native TCP socket API for Android & iOS with SSL/TLS support",
66
"main": "src/index.js",
77
"types": "lib/types/index.d.ts",

0 commit comments

Comments
 (0)