Skip to content

Commit 330d8e0

Browse files
committed
Merge branch 'master' of https://github.com/Rapsssito/react-native-tcp-socket into master
2 parents edc8518 + 3823cb5 commit 330d8e0

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

.github/workflows/stale.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ jobs:
1212
repo-token: ${{ secrets.GITHUB_TOKEN }}
1313
stale-issue-message: 'Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.'
1414
stale-issue-label: 'stale'
15+
exempt-issue-labels: 'work-in-progress'
1516
days-before-stale: 30
1617
days-before-close: 5

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [4.3.0](https://github.com/Rapsssito/react-native-tcp-socket/compare/v4.2.0...v4.3.0) (2020-08-12)
2+
3+
4+
### Features
5+
6+
* Add socket.setEncoding() method ([#74](https://github.com/Rapsssito/react-native-tcp-socket/issues/74)) ([bfa80ea](https://github.com/Rapsssito/react-native-tcp-socket/commit/bfa80ea3b690f8a486882b4921397633957f4686))
7+
18
# [4.2.0](https://github.com/Rapsssito/react-native-tcp-socket/compare/v4.1.0...v4.2.0) (2020-07-06)
29

310

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,24 @@ const client = TcpSocket.createConnection({
179179
_Note: In order to use self-signed certificates make sure to [update your metro.config.js configuration](#self-signed-ssl-only-available-for-react-native--060)._
180180

181181
## API
182-
Here are listed all methods implemented in `react-native-tcp-socket`, their functionalities are equivalent to those provided by Node's [net](https://nodejs.org/api/net.html) (more info on [#41](https://github.com/Rapsssito/react-native-tcp-socket/issues/41)). However, the **methods whose interface differs from Node are shown in bold**.
182+
Here are listed all methods implemented in `react-native-tcp-socket`, their functionalities are equivalent to those provided by Node's [net](https://nodejs.org/api/net.html) (more info on [#41](https://github.com/Rapsssito/react-native-tcp-socket/issues/41)). However, the **methods whose interface differs from Node are marked in bold**.
183183

184184
### TcpSocket
185185
* **Methods:**
186186
* **[`TcpSocket.createConnection(options[, callback])`](#createconnection)**
187187
* [`address()`](https://nodejs.org/api/net.html#net_socket_address)
188188
* [`destroy([error])`](https://nodejs.org/api/net.html#net_socket_destroy_error)
189189
* [`end([data][, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback)
190+
* [`setEncoding([encoding])`](https://nodejs.org/api/net.html#net_socket_setencoding_encoding)
190191
* [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) - _`initialDelay` is ignored_
191192
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
192193
* [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
193194
* [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback)
195+
* **Events:**
196+
* [`'close'`](https://nodejs.org/api/net.html#net_event_close_1)
197+
* [`'connect'`](https://nodejs.org/api/net.html#net_event_connect)
198+
* [`'data'`](https://nodejs.org/api/net.html#net_event_data)
199+
* [`'error'`](https://nodejs.org/api/net.html#net_event_error_1)
194200

195201
#### `createConnection()`
196202
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).

coverage/coverage-final.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/types/TcpServer.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class TcpServer extends TcpSocket {
4242
tlsCert?: any;
4343
}, callback?: ((address: string) => void) | undefined): TcpServer;
4444
setTimeout(timeout: number, callback?: (() => void) | undefined): TcpServer;
45+
setEncoding(encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined): TcpServer;
4546
setNoDelay(noDelay?: boolean): TcpServer;
4647
setKeepAlive(enable?: boolean, initialDelay?: number): TcpServer;
4748
addListener(event: string | symbol, listener: (...args: any[]) => void): TcpServer;

lib/types/TcpSocket.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class TcpSocket extends EventEmitter {
2828
_timeout: NodeJS.Timeout | undefined;
2929
/** @type {number} */
3030
_state: number;
31+
_encoding: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined;
3132
/**
3233
* @protected
3334
*/
@@ -80,6 +81,16 @@ export default class TcpSocket extends EventEmitter {
8081
* @private
8182
*/
8283
private _clearTimeout;
84+
/**
85+
* 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.
86+
* Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects.
87+
*
88+
* For instance, calling `socket.setEncoding('utf8')` will cause the output data to be interpreted as UTF-8 data, and passed as strings.
89+
* Calling `socket.setEncoding('hex')` will cause the data to be encoded in hexadecimal string format.
90+
*
91+
* @param {BufferEncoding} [encoding]
92+
*/
93+
setEncoding(encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined): TcpSocket;
8394
/**
8495
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
8596
*

package.json

Lines changed: 1 addition & 1 deletion
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": "4.2.0",
4+
"version": "4.3.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",

src/TcpSocket.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class TcpSocket extends EventEmitter {
4242
this._timeout = undefined;
4343
/** @type {number} */
4444
this._state = STATE.DISCONNECTED;
45+
this._encoding = undefined;
4546
this._registerEvents();
4647
if (address != undefined) this._setConnected(address);
4748
}
@@ -54,7 +55,8 @@ export default class TcpSocket extends EventEmitter {
5455
this._dataListener = this._eventEmitter.addListener('data', (evt) => {
5556
if (evt.id !== this._id) return;
5657
const bufferTest = Buffer.from(evt.data, 'base64');
57-
this.emit('data', bufferTest);
58+
const finalData = this._encoding ? bufferTest.toString(this._encoding) : bufferTest;
59+
this.emit('data', finalData);
5860
});
5961
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
6062
if (evt.id !== this._id) return;
@@ -155,6 +157,20 @@ export default class TcpSocket extends EventEmitter {
155157
}
156158
}
157159

160+
/**
161+
* 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.
162+
* Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects.
163+
*
164+
* For instance, calling `socket.setEncoding('utf8')` will cause the output data to be interpreted as UTF-8 data, and passed as strings.
165+
* Calling `socket.setEncoding('hex')` will cause the data to be encoded in hexadecimal string format.
166+
*
167+
* @param {BufferEncoding} [encoding]
168+
*/
169+
setEncoding(encoding) {
170+
this._encoding = encoding;
171+
return this;
172+
}
173+
158174
/**
159175
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
160176
*

0 commit comments

Comments
 (0)