@react-native-ohos/react-native-tcp-socket 6.3.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +13 -0
  4. package/harmony/tcp_socket/BuildProfile.ets +17 -0
  5. package/harmony/tcp_socket/Index.ets +6 -0
  6. package/harmony/tcp_socket/build-profile.json5 +31 -0
  7. package/harmony/tcp_socket/consumer-rules.txt +0 -0
  8. package/harmony/tcp_socket/hvigorfile.ts +6 -0
  9. package/harmony/tcp_socket/obfuscation-rules.txt +23 -0
  10. package/harmony/tcp_socket/oh-package.json5 +11 -0
  11. package/harmony/tcp_socket/src/main/cpp/CMakeLists.txt +9 -0
  12. package/harmony/tcp_socket/src/main/cpp/TcpSocketPackage.h +19 -0
  13. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/BaseReactNativeTcpSocketPackage.h +72 -0
  14. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.cpp +28 -0
  15. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.h +16 -0
  16. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ComponentDescriptors.h +24 -0
  17. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.cpp +16 -0
  18. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.h +17 -0
  19. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.cpp +19 -0
  20. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.h +18 -0
  21. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.cpp +17 -0
  22. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.h +23 -0
  23. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.cpp +16 -0
  24. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.h +19 -0
  25. package/harmony/tcp_socket/src/main/ets/Logger.ts +46 -0
  26. package/harmony/tcp_socket/src/main/ets/TcpEventListener.ts +121 -0
  27. package/harmony/tcp_socket/src/main/ets/TcpSocket.ts +17 -0
  28. package/harmony/tcp_socket/src/main/ets/TcpSocketClient.ts +444 -0
  29. package/harmony/tcp_socket/src/main/ets/TcpSocketPackage.ts +27 -0
  30. package/harmony/tcp_socket/src/main/ets/TcpSocketServer.ts +151 -0
  31. package/harmony/tcp_socket/src/main/ets/TcpSocketTurboModule.ts +224 -0
  32. package/harmony/tcp_socket/src/main/ets/generated/components/ts.ts +5 -0
  33. package/harmony/tcp_socket/src/main/ets/generated/index.ets +5 -0
  34. package/harmony/tcp_socket/src/main/ets/generated/ts.ts +6 -0
  35. package/harmony/tcp_socket/src/main/ets/generated/turboModules/TcpSocketModule.ts +38 -0
  36. package/harmony/tcp_socket/src/main/ets/generated/turboModules/ts.ts +5 -0
  37. package/harmony/tcp_socket/src/main/module.json5 +11 -0
  38. package/harmony/tcp_socket/src/main/resources/base/element/string.json +8 -0
  39. package/harmony/tcp_socket/src/main/resources/en_US/element/string.json +8 -0
  40. package/harmony/tcp_socket/src/main/resources/zh_CN/element/string.json +8 -0
  41. package/harmony/tcp_socket/src/ohosTest/ets/test/Ability.test.ets +35 -0
  42. package/harmony/tcp_socket/src/ohosTest/ets/test/List.test.ets +5 -0
  43. package/harmony/tcp_socket/src/ohosTest/module.json5 +13 -0
  44. package/harmony/tcp_socket/src/test/List.test.ets +5 -0
  45. package/harmony/tcp_socket/src/test/LocalUnit.test.ets +33 -0
  46. package/harmony/tcp_socket/ts.ts +7 -0
  47. package/harmony/tcp_socket.har +0 -0
  48. package/lib/types/Globals.d.ts +2 -0
  49. package/lib/types/Server.d.ts +138 -0
  50. package/lib/types/Socket.d.ts +272 -0
  51. package/lib/types/TLSServer.d.ts +28 -0
  52. package/lib/types/TLSSocket.d.ts +50 -0
  53. package/lib/types/index.d.ts +81 -0
  54. package/package.json +77 -0
  55. package/src/Globals.js +12 -0
  56. package/src/Server.js +271 -0
  57. package/src/Socket.js +513 -0
  58. package/src/TLSServer.js +70 -0
  59. package/src/TLSSocket.js +93 -0
  60. package/src/TcpSocketModule.ts +40 -0
  61. package/src/index.js +145 -0
package/src/Server.js ADDED
@@ -0,0 +1,271 @@
1
+ 'use strict';
2
+
3
+ import { TurboModuleRegistry, NativeModules } from 'react-native';
4
+ import EventEmitter from 'eventemitter3';
5
+ import Socket from './Socket';
6
+ import { nativeEventEmitter, getNextId } from './Globals';
7
+
8
+ const Sockets = TurboModuleRegistry ? TurboModuleRegistry.get('TcpSocketModule') : NativeModules.TcpSockets;
9
+
10
+ /**
11
+ * @typedef {object} ServerOptions
12
+ * @property {boolean} [noDelay]
13
+ * @property {boolean} [keepAlive]
14
+ * @property {number} [keepAliveInitialDelay]
15
+ * @property {boolean} [allowHalfOpen]
16
+ * @property {boolean} [pauseOnConnect]
17
+ *
18
+ * @typedef {import('./TLSSocket').default} TLSSocket
19
+ *
20
+ * @typedef {object} ServerEvents
21
+ * @property {() => void} close
22
+ * @property {(socket: Socket) => void} connection
23
+ * @property {() => void} listening
24
+ * @property {(err: Error) => void} error
25
+ * @property {(tlsSocket: TLSSocket) => void} secureConnection
26
+ *
27
+ * @extends {EventEmitter<ServerEvents, any>}
28
+ */
29
+ export default class Server extends EventEmitter {
30
+ /**
31
+ * @param {ServerOptions | ((socket: Socket) => void)} [options] Server options or connection listener
32
+ * @param {(socket: Socket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
33
+ */
34
+ constructor(options, connectionCallback) {
35
+ super();
36
+ /** @protected @readonly */
37
+ this._id = getNextId();
38
+ /** @protected @readonly */
39
+ this._eventEmitter = nativeEventEmitter;
40
+ /** @private @type {Set<Socket>} */
41
+ this._connections = new Set();
42
+ /** @private */
43
+ this._localAddress = undefined;
44
+ /** @private */
45
+ this._localPort = undefined;
46
+ /** @private */
47
+ this._localFamily = undefined;
48
+ /** @private @type {ServerOptions} */
49
+ this._serverOptions = {};
50
+ this.listening = false;
51
+
52
+ // Handle optional options argument
53
+ if (typeof options === 'function') {
54
+ /** @type {(socket: Socket) => void} */
55
+ const callback = options;
56
+ this.on('connection', callback);
57
+ options = {};
58
+ } else if (options && typeof options === 'object') {
59
+ this._serverOptions = { ...options };
60
+ if (typeof connectionCallback === 'function') {
61
+ this.on('connection', connectionCallback);
62
+ }
63
+ }
64
+
65
+ this._registerEvents();
66
+ this.on('close', this._setDisconnected, this);
67
+ }
68
+
69
+ /**
70
+ * Start a server listening for connections.
71
+ *
72
+ * This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
73
+ * The last parameter `callback` will be added as a listener for the `'listening'` event.
74
+ *
75
+ * The `server.listen()` method can be called again if and only if there was an error during the first
76
+ * `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
77
+ * error will be thrown.
78
+ *
79
+ * @param {{ port: number; host?: string; reuseAddress?: boolean} | number} options Options or port
80
+ * @param {string | (() => void)} [callback_or_host] Callback or host string
81
+ * @param {() => void} [callback] Callback function
82
+ * @returns {Server}
83
+ */
84
+ listen(options, callback_or_host, callback) {
85
+ if (this._localAddress !== undefined) throw new Error('ERR_SERVER_ALREADY_LISTEN');
86
+
87
+ /** @type {{ port: number; host: string; reuseAddress?: boolean }} */
88
+ let listenOptions = { port: 0, host: '0.0.0.0' };
89
+ /** @type {(() => void) | undefined} */
90
+ let cb;
91
+
92
+ // Handle different argument patterns
93
+ if (typeof options === 'number') {
94
+ // listen(port, [host], [callback])
95
+ listenOptions.port = options;
96
+ if (typeof callback_or_host === 'string') {
97
+ listenOptions.host = callback_or_host;
98
+ cb = callback;
99
+ } else if (typeof callback_or_host === 'function') {
100
+ cb = callback_or_host;
101
+ }
102
+ } else if (typeof options === 'object') {
103
+ // listen(options, [callback])
104
+ listenOptions = {
105
+ port: options.port,
106
+ host: options.host || '0.0.0.0',
107
+ reuseAddress: options.reuseAddress,
108
+ };
109
+ if (typeof callback_or_host === 'function') {
110
+ cb = callback_or_host;
111
+ }
112
+ } else {
113
+ throw new TypeError('options must be an object or a number');
114
+ }
115
+
116
+ // Add callback as a listener for the listening event
117
+ if (typeof cb === 'function') {
118
+ this.once('listening', cb);
119
+ }
120
+
121
+ this.once('listening', () => {
122
+ this.listening = true;
123
+ });
124
+
125
+ Sockets.listen(this._id, listenOptions);
126
+ return this;
127
+ }
128
+
129
+ /**
130
+ * Asynchronously get the number of concurrent connections on the server.
131
+ *
132
+ * Callback should take two arguments `err` and `count`.
133
+ *
134
+ * @param {(err: Error | null, count: number) => void} callback
135
+ * @returns {Server}
136
+ */
137
+ getConnections(callback) {
138
+ callback(null, this._connections.size);
139
+ return this;
140
+ }
141
+
142
+ /**
143
+ * Stops the server from accepting new connections and keeps existing connections.
144
+ * This function is asynchronous, the server is finally closed when all connections are ended and the server emits a `'close'` event.
145
+ * The optional callback will be called once the `'close'` event occurs. Unlike that event, it will be called with an `Error` as its
146
+ * only argument if the server was not open when it was closed.
147
+ *
148
+ * @param {(err?: Error) => void} [callback] Called when the server is closed.
149
+ * @returns {Server}
150
+ */
151
+ close(callback) {
152
+ if (!this._localAddress) {
153
+ callback?.(new Error('ERR_SERVER_NOT_RUNNING'));
154
+ return this;
155
+ }
156
+ if (callback) this.once('close', callback);
157
+ this.listening = false;
158
+ Sockets.close(this._id);
159
+ if (this._connections.size === 0) this.emit('close');
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Returns the bound `address`, the address `family` name, and `port` of the server as reported by the operating system if listening
165
+ * on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
166
+ * `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
167
+ *
168
+ * @returns {import('./Socket').AddressInfo | null}
169
+ */
170
+ address() {
171
+ if (!this._localAddress) return null;
172
+ return { address: this._localAddress, port: this._localPort, family: this._localFamily };
173
+ }
174
+
175
+ ref() {
176
+ console.warn('react-native-tcp-socket: Server.ref() method will have no effect.');
177
+ return this;
178
+ }
179
+
180
+ unref() {
181
+ console.warn('react-native-tcp-socket: Server.unref() method will have no effect.');
182
+ return this;
183
+ }
184
+
185
+ /**
186
+ * @private
187
+ */
188
+ _registerEvents() {
189
+ this._listeningListener = this._eventEmitter.addListener('listening', (evt) => {
190
+ if (evt.id !== this._id) return;
191
+ this._localAddress = evt.connection.localAddress;
192
+ this._localPort = evt.connection.localPort;
193
+ this._localFamily = evt.connection.localFamily;
194
+ this.emit('listening');
195
+ });
196
+ this._errorListener = this._eventEmitter.addListener('error', (evt) => {
197
+ if (evt.id !== this._id) return;
198
+ this.close();
199
+ this.emit('error', evt.error);
200
+ });
201
+ this._connectionsListener = this._eventEmitter.addListener('connection', (evt) => {
202
+ if (evt.id !== this._id) return;
203
+ const newSocket = this._buildSocket(evt.info);
204
+ this._addConnection(newSocket);
205
+ this.emit('connection', newSocket);
206
+ });
207
+ }
208
+
209
+ /**
210
+ * @private
211
+ */
212
+ _setDisconnected() {
213
+ this._localAddress = undefined;
214
+ this._localPort = undefined;
215
+ this._localFamily = undefined;
216
+ }
217
+
218
+ /**
219
+ * @protected
220
+ * @param {Socket} socket
221
+ */
222
+ _addConnection(socket) {
223
+ // Emit 'close' when all connection closed
224
+ socket.on('close', () => {
225
+ this._connections.delete(socket);
226
+ if (!this.listening && this._connections.size === 0) this.emit('close');
227
+ });
228
+ this._connections.add(socket);
229
+ }
230
+
231
+ /**
232
+ * @protected
233
+ * @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; }} info
234
+ * @returns {Socket}
235
+ */
236
+ _buildSocket(info) {
237
+ const newSocket = new Socket();
238
+ newSocket._setId(info.id);
239
+ newSocket._setConnected(info.connection);
240
+
241
+ // Apply server options to the socket if they exist
242
+ if (this._serverOptions) {
243
+ if (this._serverOptions.noDelay !== undefined) {
244
+ newSocket.setNoDelay(this._serverOptions.noDelay);
245
+ }
246
+
247
+ if (this._serverOptions.keepAlive !== undefined) {
248
+ const keepAliveDelay = this._serverOptions.keepAliveInitialDelay || 0;
249
+ newSocket.setKeepAlive(this._serverOptions.keepAlive, keepAliveDelay);
250
+ }
251
+ }
252
+
253
+ return newSocket;
254
+ }
255
+
256
+ /**
257
+ * Apply server socket options to a newly connected socket
258
+ * @param {Socket} socket
259
+ * @private
260
+ */
261
+ _applySocketOptions(socket) {
262
+ if (this._serverOptions.noDelay !== undefined) {
263
+ socket.setNoDelay(this._serverOptions.noDelay);
264
+ }
265
+
266
+ if (this._serverOptions.keepAlive !== undefined) {
267
+ const keepAliveDelay = this._serverOptions.keepAliveInitialDelay || 0;
268
+ socket.setKeepAlive(this._serverOptions.keepAlive, keepAliveDelay);
269
+ }
270
+ }
271
+ }