@react-native-ohos/react-native-tcp-socket 6.2.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 (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +13 -0
  4. package/harmony/tcp_socket/Index.ets +9 -0
  5. package/harmony/tcp_socket/build-profile.json5 +31 -0
  6. package/harmony/tcp_socket/consumer-rules.txt +0 -0
  7. package/harmony/tcp_socket/hvigorfile.ts +6 -0
  8. package/harmony/tcp_socket/obfuscation-rules.txt +23 -0
  9. package/harmony/tcp_socket/oh-package.json5 +11 -0
  10. package/harmony/tcp_socket/src/main/cpp/CMakeLists.txt +10 -0
  11. package/harmony/tcp_socket/src/main/cpp/TcpSocketPackage.h +26 -0
  12. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/BaseReactNativeTcpSocketPackage.h +65 -0
  13. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.cpp +28 -0
  14. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.h +16 -0
  15. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ComponentDescriptors.h +22 -0
  16. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.cpp +18 -0
  17. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.h +19 -0
  18. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.cpp +21 -0
  19. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.h +20 -0
  20. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.cpp +19 -0
  21. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.h +25 -0
  22. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.cpp +18 -0
  23. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.h +23 -0
  24. package/harmony/tcp_socket/src/main/ets/Logger.ts +46 -0
  25. package/harmony/tcp_socket/src/main/ets/TcpEventListener.ts +121 -0
  26. package/harmony/tcp_socket/src/main/ets/TcpSocket.ts +17 -0
  27. package/harmony/tcp_socket/src/main/ets/TcpSocketClient.ts +444 -0
  28. package/harmony/tcp_socket/src/main/ets/TcpSocketPackage.ets +27 -0
  29. package/harmony/tcp_socket/src/main/ets/TcpSocketServer.ts +151 -0
  30. package/harmony/tcp_socket/src/main/ets/TcpSocketTurboModule.ts +224 -0
  31. package/harmony/tcp_socket/src/main/ets/generated/components/ts.ts +5 -0
  32. package/harmony/tcp_socket/src/main/ets/generated/index.ets +5 -0
  33. package/harmony/tcp_socket/src/main/ets/generated/ts.ts +6 -0
  34. package/harmony/tcp_socket/src/main/ets/generated/turboModules/TcpSocketModule.ts +38 -0
  35. package/harmony/tcp_socket/src/main/ets/generated/turboModules/ts.ts +5 -0
  36. package/harmony/tcp_socket/src/main/module.json5 +11 -0
  37. package/harmony/tcp_socket/src/main/resources/base/element/string.json +8 -0
  38. package/harmony/tcp_socket/src/main/resources/en_US/element/string.json +8 -0
  39. package/harmony/tcp_socket/src/main/resources/zh_CN/element/string.json +8 -0
  40. package/harmony/tcp_socket/src/ohosTest/ets/test/Ability.test.ets +35 -0
  41. package/harmony/tcp_socket/src/ohosTest/ets/test/List.test.ets +5 -0
  42. package/harmony/tcp_socket/src/ohosTest/module.json5 +13 -0
  43. package/harmony/tcp_socket/src/test/List.test.ets +5 -0
  44. package/harmony/tcp_socket/src/test/LocalUnit.test.ets +33 -0
  45. package/harmony/tcp_socket/ts.ets +7 -0
  46. package/harmony/tcp_socket.har +0 -0
  47. package/lib/types/Globals.d.ts +2 -0
  48. package/lib/types/Server.d.ts +114 -0
  49. package/lib/types/Socket.d.ts +272 -0
  50. package/lib/types/TLSServer.d.ts +28 -0
  51. package/lib/types/TLSSocket.d.ts +50 -0
  52. package/lib/types/index.d.ts +62 -0
  53. package/package.json +90 -0
  54. package/src/Globals.js +12 -0
  55. package/src/Server.js +185 -0
  56. package/src/Socket.js +513 -0
  57. package/src/TLSServer.js +70 -0
  58. package/src/TLSSocket.js +93 -0
  59. package/src/TcpSocketModule.ts +40 -0
  60. package/src/index.js +131 -0
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ import { Image } from 'react-native';
4
+ import Server from './Server';
5
+ import TLSSocket from './TLSSocket';
6
+
7
+ /**
8
+ * @typedef {object} TLSServerOptions
9
+ * @property {any} keystore
10
+ *
11
+ * @extends {Server}
12
+ */
13
+ export default class TLSServer extends Server {
14
+ /**
15
+ * @param {(socket: TLSSocket) => void} [secureConnectionListener] Automatically set as a listener for the `'secureConnection'` event.
16
+ */
17
+ constructor(secureConnectionListener) {
18
+ super();
19
+ if (secureConnectionListener) this.on('secureConnection', secureConnectionListener);
20
+ this._registerTLSEvents();
21
+ }
22
+
23
+ /**
24
+ * @param {TLSServerOptions} options TLS server options
25
+ */
26
+ setSecureContext(options) {
27
+ /** @private */
28
+ this._options = { ...options };
29
+ this._options.keystore = Image.resolveAssetSource(this._options.keystore).uri;
30
+ }
31
+
32
+ /**
33
+ * Start a server listening for connections.
34
+ *
35
+ * This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
36
+ * The last parameter `callback` will be added as a listener for the `'listening'` event.
37
+ *
38
+ * The `server.listen()` method can be called again if and only if there was an error during the first
39
+ * `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
40
+ * error will be thrown.
41
+ *
42
+ * @param {{ port: number; host: string; reuseAddress?: boolean}} options
43
+ * @param {() => void} [callback]
44
+ * @override
45
+ */
46
+ listen(options, callback) {
47
+ const newOptions = { ...options };
48
+ // @ts-ignore
49
+ newOptions['tls'] = this._options;
50
+ return super.listen(newOptions, callback);
51
+ }
52
+
53
+ /**
54
+ * @private
55
+ */
56
+ _registerTLSEvents() {
57
+ this._secureConnectionListener = this._eventEmitter.addListener(
58
+ 'secureConnection',
59
+ (evt) => {
60
+ if (evt.id !== this._id) return;
61
+ const standardSocket = this._buildSocket(evt.info);
62
+ standardSocket._unregisterEvents();
63
+ const tlsSocket = new TLSSocket(standardSocket);
64
+ this._addConnection(tlsSocket);
65
+ this.emit('connection', standardSocket);
66
+ this.emit('secureConnection', tlsSocket);
67
+ }
68
+ );
69
+ }
70
+ }
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ import { Image, NativeModules, TurboModuleRegistry } from 'react-native';
4
+ const Sockets = TurboModuleRegistry ? TurboModuleRegistry.get('TcpSocketModule') : NativeModules.TcpSockets;
5
+ import Socket from './Socket';
6
+
7
+ /**
8
+ * @typedef {object} TLSSocketOptions
9
+ * @property {any} [ca]
10
+ * @property {any} [key]
11
+ * @property {any} [cert]
12
+ * @property {string} [androidKeyStore]
13
+ * @property {string} [certAlias]
14
+ * @property {string} [keyAlias]
15
+ * @property {string[]} [resolvedKeys]
16
+ *
17
+ * @extends {Socket}
18
+ */
19
+ export default class TLSSocket extends Socket {
20
+ /**
21
+ * @param {Socket} socket Any instance of `Socket`.
22
+ * @param {TLSSocketOptions} [options] Options for the TLS socket.
23
+ */
24
+ constructor(socket, options = {}) {
25
+ super();
26
+ /** @private */
27
+ this._options = { ...options };
28
+ TLSSocket.resolveAssetIfNeeded(this._options, 'ca');
29
+ TLSSocket.resolveAssetIfNeeded(this._options, 'key');
30
+ TLSSocket.resolveAssetIfNeeded(this._options, 'cert');
31
+
32
+ /** @private */
33
+ this._socket = socket;
34
+ // @ts-ignore
35
+ this._setId(this._socket._id);
36
+ this._startTLS();
37
+ if (socket.pending || socket.connecting) socket.once('connect', () => this._initialize());
38
+ else this._initialize();
39
+ }
40
+
41
+ /**
42
+ * @private
43
+ */
44
+ _initialize() {
45
+ // Avoid calling twice destroy() if an error occurs
46
+ this._socket._errorListener?.remove();
47
+ this.on('error', (error) => this._socket.emit('error', error));
48
+ this._setConnected({
49
+ // @ts-ignore
50
+ localAddress: this._socket.localAddress,
51
+ // @ts-ignore
52
+ localPort: this._socket.localPort,
53
+ // @ts-ignore
54
+ remoteAddress: this._socket.remoteAddress,
55
+ // @ts-ignore
56
+ remotePort: this._socket.remotePort,
57
+ // @ts-ignore
58
+ remoteFamily: this._socket.remoteFamily,
59
+ });
60
+ }
61
+
62
+ /**
63
+ * @private
64
+ */
65
+ _startTLS() {
66
+ Sockets.startTLS(this._id, this._options);
67
+ }
68
+
69
+ getCertificate() {
70
+ return Sockets.getCertificate(this._id);
71
+ }
72
+
73
+ getPeerCertificate() {
74
+ return Sockets.getPeerCertificate(this._id);
75
+ }
76
+
77
+ /**
78
+ * @private
79
+ * Resolves the asset source if necessary and registers the resolved key.
80
+ * @param {TLSSocketOptions} options The options object containing the source to be resolved.
81
+ * @param {'ca' | 'key' | 'cert'} key The key name being resolved.
82
+ */
83
+ static resolveAssetIfNeeded(options, key) {
84
+ const source = options[key];
85
+ if (source && typeof source !== 'string') {
86
+ if (!options.resolvedKeys) {
87
+ options.resolvedKeys = [];
88
+ }
89
+ options.resolvedKeys.push(key);
90
+ options[key] = Image.resolveAssetSource(source).uri;
91
+ }
92
+ }
93
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
8
+ import { TurboModuleRegistry } from 'react-native';
9
+
10
+ export interface Spec extends TurboModule {
11
+
12
+ listen(cId: number, options: Object): void;
13
+
14
+ close(cid: number): void;
15
+
16
+ destroy(cid: number): void;
17
+
18
+ end(cid: number): void;
19
+
20
+ pause(cid: number): void;
21
+
22
+ resume(cid: number): void;
23
+
24
+ connect(cId: number, host: string, port: number, options: Object): void;
25
+
26
+ startTLS(cId: number, tlsOptions: Object): void;
27
+
28
+ write(cId: number, base64String: string, msgId: number): void;
29
+
30
+ setNoDelay(cId: number, noDelay: boolean): void;
31
+
32
+ setKeepAlive(cId: number, enable: boolean, initialDelay: number): void;
33
+
34
+ getPeerCertificate(cId: number): Promise<string>;
35
+
36
+ getCertificate(cId: number): Promise<string>;
37
+ }
38
+
39
+
40
+ export default TurboModuleRegistry.get<Spec>('TcpSocketModule') as Spec | null;
package/src/index.js ADDED
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ import Socket from './Socket';
4
+ import Server from './Server';
5
+ import TLSServer from './TLSServer';
6
+ import TLSSocket from './TLSSocket';
7
+
8
+ /**
9
+ * @param {(socket: Socket) => void} connectionListener
10
+ * @returns {Server}
11
+ */
12
+ function createServer(connectionListener) {
13
+ return new Server(connectionListener);
14
+ }
15
+
16
+ /**
17
+ * @param {import('./TLSServer').TLSServerOptions} options
18
+ * @param {(socket: TLSSocket) => void} connectionListener
19
+ * @returns {TLSServer}
20
+ */
21
+ function createTLSServer(options, connectionListener) {
22
+ const server = new TLSServer(connectionListener);
23
+ server.setSecureContext(options);
24
+ return server;
25
+ }
26
+
27
+ /**
28
+ * The `callback` function, if specified, will be added as a listener for the `'secureConnect'` event.
29
+ *
30
+ * @param {import('./TLSSocket').TLSSocketOptions & import('./Socket').ConnectionOptions} options
31
+ * @param {() => void} [callback]
32
+ * @returns {TLSSocket}
33
+ */
34
+ function connectTLS(options, callback) {
35
+ const socket = new Socket();
36
+ const tlsSocket = new TLSSocket(socket, options);
37
+ socket.once('connect', () => tlsSocket.emit('secureConnect'));
38
+ if (callback) tlsSocket.once('secureConnect', callback);
39
+ socket.connect(options);
40
+ return tlsSocket;
41
+ }
42
+
43
+ /**
44
+ * @param {import('./Socket').ConnectionOptions} options
45
+ * @param {() => void} callback
46
+ * @returns {Socket}
47
+ */
48
+ function createConnection(options, callback) {
49
+ const tcpSocket = new Socket();
50
+ return tcpSocket.connect(options, callback);
51
+ }
52
+
53
+ // IPv4 Segment
54
+ const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
55
+ const v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
56
+ const IPv4Reg = new RegExp(`^${v4Str}$`);
57
+
58
+ // IPv6 Segment
59
+ const v6Seg = '(?:[0-9a-fA-F]{1,4})';
60
+ const IPv6Reg = new RegExp(
61
+ '^(' +
62
+ `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
63
+ `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
64
+ `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
65
+ `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
66
+ `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
67
+ `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
68
+ `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
69
+ `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
70
+ ')(%[0-9a-zA-Z-.:]{1,})?$'
71
+ );
72
+
73
+ /**
74
+ * Returns `true` if input is a version 4 IP address, otherwise returns `false`.
75
+ *
76
+ * @param {string} input
77
+ */
78
+ function isIPv4(input) {
79
+ return IPv4Reg.test(input);
80
+ }
81
+
82
+ /**
83
+ * Returns `true` if input is a version 6 IP address, otherwise returns `false`.
84
+ *
85
+ * @param {string} input
86
+ */
87
+ function isIPv6(input) {
88
+ return IPv6Reg.test(input);
89
+ }
90
+
91
+ /**
92
+ * Tests if input is an IP address. Returns `0` for invalid strings, returns `4` for IP version 4 addresses, and returns `6` for IP version 6 addresses.
93
+ *
94
+ * @param {string} input
95
+ */
96
+ function isIP(input) {
97
+ if (isIPv4(input)) return 4;
98
+ else if (isIPv6(input)) return 6;
99
+ return 0;
100
+ }
101
+
102
+ export default {
103
+ connect: createConnection,
104
+ createServer,
105
+ createConnection,
106
+ createTLSServer,
107
+ connectTLS,
108
+ isIP,
109
+ isIPv4,
110
+ isIPv6,
111
+ Server,
112
+ Socket,
113
+ TLSServer,
114
+ TLSSocket,
115
+ };
116
+
117
+ // @ts-ignore
118
+ module.exports = {
119
+ connect: createConnection,
120
+ createServer,
121
+ createConnection,
122
+ createTLSServer,
123
+ connectTLS,
124
+ isIP,
125
+ isIPv4,
126
+ isIPv6,
127
+ Server,
128
+ Socket,
129
+ TLSServer,
130
+ TLSSocket,
131
+ };