react-native-nitro-net 0.2.0 → 0.3.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 (83) hide show
  1. package/README.md +70 -12
  2. package/android/libs/arm64-v8a/librust_c_net.so +0 -0
  3. package/android/libs/armeabi-v7a/librust_c_net.so +0 -0
  4. package/android/libs/x86/librust_c_net.so +0 -0
  5. package/android/libs/x86_64/librust_c_net.so +0 -0
  6. package/cpp/HybridHttpParser.hpp +67 -0
  7. package/cpp/HybridNetDriver.hpp +6 -0
  8. package/cpp/HybridNetServerDriver.hpp +7 -0
  9. package/cpp/HybridNetSocketDriver.hpp +27 -0
  10. package/cpp/NetBindings.hpp +15 -0
  11. package/ios/Frameworks/RustCNet.xcframework/Info.plist +5 -5
  12. package/ios/Frameworks/RustCNet.xcframework/ios-arm64/RustCNet.framework/RustCNet +0 -0
  13. package/ios/Frameworks/RustCNet.xcframework/ios-arm64_x86_64-simulator/RustCNet.framework/RustCNet +0 -0
  14. package/lib/Driver.d.ts +1 -0
  15. package/lib/Driver.d.ts.map +1 -0
  16. package/lib/Driver.js +2 -5
  17. package/lib/Net.nitro.d.ts +20 -0
  18. package/lib/Net.nitro.d.ts.map +1 -0
  19. package/lib/Net.nitro.js +4 -7
  20. package/lib/http.d.ts +204 -0
  21. package/lib/http.d.ts.map +1 -0
  22. package/lib/http.js +1126 -0
  23. package/lib/https.d.ts +25 -0
  24. package/lib/https.d.ts.map +1 -0
  25. package/lib/https.js +102 -0
  26. package/lib/index.d.ts +41 -160
  27. package/lib/index.d.ts.map +1 -0
  28. package/lib/index.js +11 -821
  29. package/lib/net.d.ts +197 -0
  30. package/lib/net.d.ts.map +1 -0
  31. package/lib/net.js +875 -0
  32. package/lib/tls.d.ts +23 -1
  33. package/lib/tls.d.ts.map +1 -0
  34. package/lib/tls.js +108 -54
  35. package/nitrogen/generated/android/RustCNet+autolinking.cmake +2 -0
  36. package/nitrogen/generated/android/RustCNetOnLoad.cpp +2 -0
  37. package/nitrogen/generated/android/c++/JHybridHttpParserSpec.cpp +54 -0
  38. package/nitrogen/generated/android/c++/JHybridHttpParserSpec.hpp +65 -0
  39. package/nitrogen/generated/android/c++/JHybridNetDriverSpec.cpp +9 -0
  40. package/nitrogen/generated/android/c++/JHybridNetDriverSpec.hpp +1 -0
  41. package/nitrogen/generated/android/c++/JHybridNetServerDriverSpec.cpp +4 -0
  42. package/nitrogen/generated/android/c++/JHybridNetServerDriverSpec.hpp +1 -0
  43. package/nitrogen/generated/android/c++/JHybridNetSocketDriverSpec.cpp +9 -0
  44. package/nitrogen/generated/android/c++/JHybridNetSocketDriverSpec.hpp +2 -0
  45. package/nitrogen/generated/android/c++/JNetConfig.hpp +7 -3
  46. package/nitrogen/generated/android/kotlin/com/margelo/nitro/net/HybridHttpParserSpec.kt +58 -0
  47. package/nitrogen/generated/android/kotlin/com/margelo/nitro/net/HybridNetDriverSpec.kt +4 -0
  48. package/nitrogen/generated/android/kotlin/com/margelo/nitro/net/HybridNetServerDriverSpec.kt +4 -0
  49. package/nitrogen/generated/android/kotlin/com/margelo/nitro/net/HybridNetSocketDriverSpec.kt +8 -0
  50. package/nitrogen/generated/android/kotlin/com/margelo/nitro/net/NetConfig.kt +6 -3
  51. package/nitrogen/generated/ios/RustCNet-Swift-Cxx-Bridge.cpp +17 -0
  52. package/nitrogen/generated/ios/RustCNet-Swift-Cxx-Bridge.hpp +26 -0
  53. package/nitrogen/generated/ios/RustCNet-Swift-Cxx-Umbrella.hpp +5 -0
  54. package/nitrogen/generated/ios/c++/HybridHttpParserSpecSwift.cpp +11 -0
  55. package/nitrogen/generated/ios/c++/HybridHttpParserSpecSwift.hpp +79 -0
  56. package/nitrogen/generated/ios/c++/HybridNetDriverSpecSwift.hpp +11 -0
  57. package/nitrogen/generated/ios/c++/HybridNetServerDriverSpecSwift.hpp +6 -0
  58. package/nitrogen/generated/ios/c++/HybridNetSocketDriverSpecSwift.hpp +14 -0
  59. package/nitrogen/generated/ios/swift/HybridHttpParserSpec.swift +56 -0
  60. package/nitrogen/generated/ios/swift/HybridHttpParserSpec_cxx.swift +131 -0
  61. package/nitrogen/generated/ios/swift/HybridNetDriverSpec.swift +1 -0
  62. package/nitrogen/generated/ios/swift/HybridNetDriverSpec_cxx.swift +15 -0
  63. package/nitrogen/generated/ios/swift/HybridNetServerDriverSpec.swift +1 -0
  64. package/nitrogen/generated/ios/swift/HybridNetServerDriverSpec_cxx.swift +11 -0
  65. package/nitrogen/generated/ios/swift/HybridNetSocketDriverSpec.swift +2 -0
  66. package/nitrogen/generated/ios/swift/HybridNetSocketDriverSpec_cxx.swift +36 -0
  67. package/nitrogen/generated/ios/swift/NetConfig.swift +19 -1
  68. package/nitrogen/generated/shared/c++/HybridHttpParserSpec.cpp +21 -0
  69. package/nitrogen/generated/shared/c++/HybridHttpParserSpec.hpp +63 -0
  70. package/nitrogen/generated/shared/c++/HybridNetDriverSpec.cpp +1 -0
  71. package/nitrogen/generated/shared/c++/HybridNetDriverSpec.hpp +4 -0
  72. package/nitrogen/generated/shared/c++/HybridNetServerDriverSpec.cpp +1 -0
  73. package/nitrogen/generated/shared/c++/HybridNetServerDriverSpec.hpp +1 -0
  74. package/nitrogen/generated/shared/c++/HybridNetSocketDriverSpec.cpp +2 -0
  75. package/nitrogen/generated/shared/c++/HybridNetSocketDriverSpec.hpp +2 -0
  76. package/nitrogen/generated/shared/c++/NetConfig.hpp +6 -2
  77. package/package.json +9 -7
  78. package/src/Net.nitro.ts +17 -0
  79. package/src/http.ts +1303 -0
  80. package/src/https.ts +125 -0
  81. package/src/index.ts +13 -874
  82. package/src/net.ts +1005 -0
  83. package/src/tls.ts +82 -6
package/README.md CHANGED
@@ -1,15 +1,20 @@
1
1
  # react-native-nitro-net
2
2
 
3
- Node.js `net` API implementation for React Native using [Nitro Modules](https://github.com/mrousavy/nitro) and Rust.
3
+ Ultra-high-performance networking to React Native by combining a memory-safe Rust core with the zero-overhead Nitro Modules JSI bridge. Provides Node.js-compatible net, tls, http(s) API.
4
+
5
+ [![license](https://img.shields.io/badge/license-ISC-blue.svg)]()
6
+ [![platform](https://img.shields.io/badge/platform-ios%20%7C%20android-lightgrey.svg)]()
7
+ [![compatibility](https://img.shields.io/badge/Node.js-100%25%20dns-green.svg)]()
8
+ [中文文档](./README_zh.md)
4
9
 
5
10
  ## Features
6
11
 
7
12
  * 🚀 **High Performance**: Built on top of Rust's `tokio` asynchronous runtime.
8
- * 🤝 **Node.js Compatible**: Implements standard `net` and `tls` APIs including `Socket`, `Server`, `TLSSocket`, and `SecureContext`.
13
+ * 🤝 **Node.js Compatible**: Implements standard `net`, `tls`, `http`, and `https` APIs.
9
14
  * 🛡️ **Modern Security**: TLS implementation powered by **Rustls 0.23** (Ring provider), supporting TLS 1.2 and 1.3.
10
- * 🔒 **Full TLS Support**: Support for PEM/PFX certificates, encrypted private keys, SNI, Session tickets, and 100% Node.js API surface compatibility.
15
+ * 🔒 **Full Protocol Support**: Support for PEM/PFX certificates, SNI, HTTP Trailers, 100 Continue, Protocol Upgrades (101), and HTTP Tunneling (CONNECT).
11
16
  * ⚡ **Nitro Modules**: Uses JSI for zero-overhead communication between JavaScript and Native code.
12
- * 🛡️ **Robust & Stable**: Advanced fixes for port reuse, deadlocks, and DNS reliability.
17
+ * 🛡️ **Robust & Stable**: Advanced fixes for port reuse, deadlocks, and connection pooling hangs.
13
18
  * 📱 **Cross-Platform**: Supports both iOS and Android.
14
19
 
15
20
  ## Installation
@@ -100,14 +105,66 @@ const server = tls.createServer({
100
105
  server.listen(443);
101
106
  ```
102
107
 
103
- ## Stability Improvements
108
+ * **Advanced Features**: Supports `keylog` event re-emission for Wireshark, session resumption, and `asyncDispose`.
109
+ * **Performance Tuning**: Configurable `headersTimeout`, `keepAliveTimeout`, and `requestTimeout`.
110
+ * **Resource Management**: Strict protective shutdown logic in Rust to prevent socket and Unix domain socket file leaks.
111
+
112
+ ## Usage
104
113
 
105
- We have implemented several critical fixes to ensure production-grade stability:
114
+ ### HTTP Request
106
115
 
107
- * **Port Reuse (`SO_REUSEPORT`)**: Automatically enabled on Unix/iOS to allow immediate server restarts without the "Address already in use" error.
108
- * **Anti-Deadlock Logic**: C++ layer uses lock-free callback dispatching to prevent UI freezes during high-frequency events.
109
- * **DNS Reliability**: Automatically retries all resolved IP addresses if the first one fails to connect.
110
- * **Resource Management**: Strict protective shutdown logic in Rust to prevent socket and Unix domain socket file leaks.
116
+ Implementation of the standard Node.js `http` API.
117
+
118
+ ```typescript
119
+ import { http } from 'react-native-nitro-net';
120
+
121
+ http.get('http://google.com', (res) => {
122
+ console.log(`Status: ${res.statusCode}`);
123
+ res.on('data', (chunk) => console.log(`Body segment: ${chunk.length} bytes`));
124
+ res.on('end', () => console.log('Request complete'));
125
+ });
126
+ ```
127
+
128
+ ### HTTPS with Connection Pooling
129
+
130
+ Uses `https` and the built-in `Agent` for connection reuse.
131
+
132
+ ```typescript
133
+ import { https } from 'react-native-nitro-net';
134
+
135
+ const agent = new https.Agent({ keepAlive: true });
136
+
137
+ https.get('https://api.github.com/users/margelo', { agent }, (res) => {
138
+ // ... handle response
139
+ });
140
+ ```
141
+
142
+ ### TCP Client (Socket)
143
+
144
+ ```typescript
145
+ import net from 'react-native-nitro-net';
146
+
147
+ const client = net.createConnection({ port: 8080, host: '127.0.0.1' }, () => {
148
+ client.write('Hello Server!');
149
+ });
150
+ ```
151
+
152
+ ### Server (Dynamic Port Support)
153
+
154
+ The server supports binding to a dynamic port by using `0`.
155
+
156
+ ```typescript
157
+ import net from 'react-native-nitro-net';
158
+
159
+ const server = net.createServer((socket) => {
160
+ socket.write('Echo: ' + socket.read());
161
+ });
162
+
163
+ server.listen(0, '127.0.0.1', () => {
164
+ const address = server.address();
165
+ console.log(`Server listening on dynamic port: ${address?.port}`);
166
+ });
167
+ ```
111
168
 
112
169
  ## Compatibility Notes
113
170
 
@@ -147,7 +204,7 @@ We have implemented several critical fixes to ensure production-grade stability:
147
204
 
148
205
  | Method | Description |
149
206
  | --- | --- |
150
- | `initWithConfig(options)` | Optional. Initializes the Rust runtime with custom settings (e.g., `workerThreads`). Must be called before any other operation. |
207
+ | `initWithConfig(options)` | Optional. Initializes the Rust runtime with custom settings (e.g., `workerThreads`, `debug`). Must be called before any other operation. |
151
208
  | `setVerbose(bool)` | Toggle detailed logging for JS, C++, and Rust. |
152
209
  | `isIP(string)` | Returns `0`, `4`, or `6`. |
153
210
 
@@ -159,8 +216,9 @@ We have implemented several critical fixes to ensure production-grade stability:
159
216
  | `close()` | Stops the server and **destroys all active connections**. |
160
217
  | `address()` | Returns the bound address (crucial for dynamic ports). |
161
218
  | `getConnections(cb)`| Get count of active connections. |
219
+ | `renegotiate(opt, cb)`| **Shim**: Returns `ERR_TLS_RENEGOTIATION_DISABLED` (Rustls security policy). |
162
220
 
163
- **Events**: `listening`, `connection`, `error`, `close`.
221
+ **Events**: `listening`, `connection`, `error`, `close`, `connect` (HTTP Tunneling).
164
222
 
165
223
  ### `tls.Server`
166
224
  *Extends `net.Server`*
Binary file
@@ -0,0 +1,67 @@
1
+ #pragma once
2
+
3
+ #include "../nitrogen/generated/shared/c++/HybridHttpParserSpec.hpp"
4
+ #include "NetBindings.hpp"
5
+ #include <NitroModules/ArrayBuffer.hpp>
6
+ #include <string>
7
+
8
+ namespace margelo {
9
+ namespace nitro {
10
+ namespace net {
11
+
12
+ using namespace margelo::nitro;
13
+
14
+ class HybridHttpParser : public HybridHttpParserSpec {
15
+ public:
16
+ HybridHttpParser(int mode) : HybridObject(TAG) {
17
+ _id = net_http_parser_create(mode);
18
+ }
19
+
20
+ ~HybridHttpParser() { net_http_parser_destroy(_id); }
21
+
22
+ std::string feed(const std::shared_ptr<ArrayBuffer> &data) override {
23
+ if (!data)
24
+ return "";
25
+
26
+ char buf[4096];
27
+ int res =
28
+ net_http_parser_feed(_id, data->data(), data->size(), buf, sizeof(buf));
29
+
30
+ if (res > 0) {
31
+ // Complete message
32
+ return std::string(buf, res);
33
+ } else if (res == 0) {
34
+ // Partial message
35
+ return "";
36
+ } else if (res < -3) {
37
+ // Buffer too small, required size is -res
38
+ size_t requiredSize = static_cast<size_t>(-res);
39
+ std::string largerBuf(requiredSize, '\0');
40
+ res = net_http_parser_feed(_id, nullptr, 0, &largerBuf[0],
41
+ requiredSize + 1);
42
+ if (res > 0) {
43
+ return std::string(largerBuf.data(), res);
44
+ }
45
+ return "ERROR: Re-parse failed after enlarging buffer";
46
+ } else {
47
+ // Error
48
+ switch (res) {
49
+ case -1:
50
+ return "ERROR: JSON serialization failed";
51
+ case -2:
52
+ return "ERROR: HTTP parse failed";
53
+ case -3:
54
+ return "ERROR: Parser not found";
55
+ default:
56
+ return "ERROR: Unknown error";
57
+ }
58
+ }
59
+ }
60
+
61
+ private:
62
+ uint32_t _id;
63
+ };
64
+
65
+ } // namespace net
66
+ } // namespace nitro
67
+ } // namespace margelo
@@ -1,6 +1,8 @@
1
1
  #pragma once
2
2
 
3
+ #include "../nitrogen/generated/shared/c++/HybridHttpParserSpec.hpp"
3
4
  #include "../nitrogen/generated/shared/c++/HybridNetDriverSpec.hpp"
5
+ #include "HybridHttpParser.hpp"
4
6
  #include "HybridNetServerDriver.hpp"
5
7
  #include "HybridNetSocketDriver.hpp"
6
8
  #include "NetManager.hpp"
@@ -36,6 +38,10 @@ public:
36
38
  return std::make_shared<HybridNetServerDriver>();
37
39
  }
38
40
 
41
+ std::shared_ptr<HybridHttpParserSpec> createHttpParser(double mode) override {
42
+ return std::make_shared<HybridHttpParser>(static_cast<int>(mode));
43
+ }
44
+
39
45
  double
40
46
  createSecureContext(const std::string &cert, const std::string &key,
41
47
  const std::optional<std::string> &passphrase) override {
@@ -63,6 +63,13 @@ public:
63
63
  net_listen_unix(_id, path.c_str(), static_cast<int>(backlog.value_or(128)));
64
64
  }
65
65
 
66
+ void listenTLSUnix(const std::string &path, double secureContextId,
67
+ std::optional<double> backlog) override {
68
+ net_listen_tls_unix(_id, path.c_str(),
69
+ static_cast<int>(backlog.value_or(128)),
70
+ static_cast<uint32_t>(secureContextId));
71
+ }
72
+
66
73
  void listenHandle(double fd, std::optional<double> backlog) override {
67
74
  net_listen_handle(_id, static_cast<int>(fd),
68
75
  static_cast<int>(backlog.value_or(128)));
@@ -4,8 +4,10 @@
4
4
  #include "NetBindings.hpp"
5
5
  #include "NetManager.hpp"
6
6
  #include <NitroModules/ArrayBuffer.hpp>
7
+ #include <memory>
7
8
  #include <optional>
8
9
  #include <string>
10
+ #include <vector>
9
11
 
10
12
  namespace margelo {
11
13
  namespace nitro {
@@ -180,6 +182,31 @@ public:
180
182
 
181
183
  void enableKeylog() override { net_socket_enable_keylog(_id); }
182
184
 
185
+ void enableTrace() override { net_socket_enable_trace(_id); }
186
+
187
+ std::optional<std::shared_ptr<ArrayBuffer>> exportKeyingMaterial(
188
+ double length, const std::string &label,
189
+ const std::optional<std::shared_ptr<ArrayBuffer>> &context) override {
190
+ size_t len = static_cast<size_t>(length);
191
+ std::vector<uint8_t> output(len);
192
+
193
+ const uint8_t *ctx_data = nullptr;
194
+ size_t ctx_len = 0;
195
+ if (context.has_value() && context.value()) {
196
+ ctx_data = context.value()->data();
197
+ ctx_len = context.value()->size();
198
+ }
199
+
200
+ int result = net_socket_export_keying_material(
201
+ _id, len, label.c_str(), ctx_data, ctx_len, output.data(),
202
+ output.size());
203
+
204
+ if (result > 0) {
205
+ return ArrayBuffer::copy(output.data(), static_cast<size_t>(result));
206
+ }
207
+ return std::nullopt;
208
+ }
209
+
183
210
  void setNoDelay(bool enable) override { net_set_nodelay(_id, enable); }
184
211
 
185
212
  void setKeepAlive(bool enable, double delay) override {
@@ -42,6 +42,13 @@ void net_socket_reset_and_destroy(uint32_t id);
42
42
  size_t net_get_ephemeral_key_info(uint32_t id, char *buf, size_t len);
43
43
  size_t net_get_shared_sigalgs(uint32_t id, char *buf, size_t len);
44
44
 
45
+ // TLS Features: enableTrace and exportKeyingMaterial
46
+ void net_socket_enable_trace(uint32_t id);
47
+ int net_socket_export_keying_material(uint32_t id, size_t length,
48
+ const char *label, const uint8_t *context,
49
+ size_t context_len, uint8_t *buf,
50
+ size_t buf_len);
51
+
45
52
  // New Options
46
53
  void net_set_nodelay(uint32_t id, bool enable);
47
54
  void net_set_keepalive(uint32_t id, bool enable, uint64_t delay_ms);
@@ -60,6 +67,8 @@ void net_shutdown(uint32_t id);
60
67
  // IPC / Unix Domain Sockets
61
68
  void net_connect_unix(uint32_t id, const char *path);
62
69
  void net_listen_unix(uint32_t id, const char *path, int backlog);
70
+ void net_listen_tls_unix(uint32_t id, const char *path, int backlog,
71
+ uint32_t secure_context_id);
63
72
 
64
73
  #if !defined(__ANDROID__)
65
74
  // Unix-only TLS functions (not available on Android)
@@ -108,6 +117,12 @@ void net_set_session(uint32_t id, const uint8_t *ticket, size_t ticket_len);
108
117
  size_t net_server_get_ticket_keys(uint32_t id, uint8_t *buf, size_t len);
109
118
  void net_server_set_ticket_keys(uint32_t id, const uint8_t *keys, size_t len);
110
119
 
120
+ // HTTP Parser
121
+ uint32_t net_http_parser_create(int mode);
122
+ int net_http_parser_feed(uint32_t id, const uint8_t *data, size_t len,
123
+ char *buf, size_t buf_len);
124
+ void net_http_parser_destroy(uint32_t id);
125
+
111
126
  // Event Types
112
127
  #define NET_EVENT_CONNECT 1
113
128
  #define NET_EVENT_DATA 2
@@ -8,32 +8,32 @@
8
8
  <key>BinaryPath</key>
9
9
  <string>RustCNet.framework/RustCNet</string>
10
10
  <key>LibraryIdentifier</key>
11
- <string>ios-arm64</string>
11
+ <string>ios-arm64_x86_64-simulator</string>
12
12
  <key>LibraryPath</key>
13
13
  <string>RustCNet.framework</string>
14
14
  <key>SupportedArchitectures</key>
15
15
  <array>
16
16
  <string>arm64</string>
17
+ <string>x86_64</string>
17
18
  </array>
18
19
  <key>SupportedPlatform</key>
19
20
  <string>ios</string>
21
+ <key>SupportedPlatformVariant</key>
22
+ <string>simulator</string>
20
23
  </dict>
21
24
  <dict>
22
25
  <key>BinaryPath</key>
23
26
  <string>RustCNet.framework/RustCNet</string>
24
27
  <key>LibraryIdentifier</key>
25
- <string>ios-arm64_x86_64-simulator</string>
28
+ <string>ios-arm64</string>
26
29
  <key>LibraryPath</key>
27
30
  <string>RustCNet.framework</string>
28
31
  <key>SupportedArchitectures</key>
29
32
  <array>
30
33
  <string>arm64</string>
31
- <string>x86_64</string>
32
34
  </array>
33
35
  <key>SupportedPlatform</key>
34
36
  <string>ios</string>
35
- <key>SupportedPlatformVariant</key>
36
- <string>simulator</string>
37
37
  </dict>
38
38
  </array>
39
39
  <key>CFBundlePackageType</key>
package/lib/Driver.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import type { NetDriver } from './Net.nitro';
2
2
  export declare const Driver: NetDriver;
3
+ //# sourceMappingURL=Driver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Driver.d.ts","sourceRoot":"","sources":["../src/Driver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE5C,eAAO,MAAM,MAAM,WAA0D,CAAA"}
package/lib/Driver.js CHANGED
@@ -1,5 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Driver = void 0;
4
- const react_native_nitro_modules_1 = require("react-native-nitro-modules");
5
- exports.Driver = react_native_nitro_modules_1.NitroModules.createHybridObject('NetDriver');
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ export const Driver = NitroModules.createHybridObject('NetDriver');
@@ -48,6 +48,8 @@ export interface NetSocketDriver extends HybridObject<{
48
48
  destroy(): void;
49
49
  resetAndDestroy(): void;
50
50
  enableKeylog(): void;
51
+ enableTrace(): void;
52
+ exportKeyingMaterial(length: number, label: string, context?: ArrayBuffer): ArrayBuffer | undefined;
51
53
  setNoDelay(enable: boolean): void;
52
54
  setKeepAlive(enable: boolean, delay: number): void;
53
55
  getLocalAddress(): string;
@@ -68,6 +70,7 @@ export interface NetServerDriver extends HybridObject<{
68
70
  listen(port: number, backlog?: number, ipv6Only?: boolean, reusePort?: boolean): void;
69
71
  listenTLS(port: number, secureContextId: number, backlog?: number, ipv6Only?: boolean, reusePort?: boolean): void;
70
72
  listenUnix(path: string, backlog?: number): void;
73
+ listenTLSUnix(path: string, secureContextId: number, backlog?: number): void;
71
74
  /**
72
75
  * Listen on an existing file descriptor (handle)
73
76
  * @param fd File descriptor of an already-bound TCP listener
@@ -78,6 +81,17 @@ export interface NetServerDriver extends HybridObject<{
78
81
  maxConnections: number;
79
82
  close(): void;
80
83
  }
84
+ export interface HttpParser extends HybridObject<{
85
+ ios: 'swift';
86
+ android: 'kotlin';
87
+ }> {
88
+ /**
89
+ * Feed data to the parser
90
+ * @param data Raw byte data from the network
91
+ * @returns JSON string of the parsed message if complete, empty string if partial, or error message starting with 'ERROR:'
92
+ */
93
+ feed(data: ArrayBuffer): string;
94
+ }
81
95
  /**
82
96
  * Runtime configuration for the network module
83
97
  */
@@ -87,6 +101,10 @@ export interface NetConfig {
87
101
  * 0 = use CPU core count (default)
88
102
  */
89
103
  workerThreads?: number;
104
+ /**
105
+ * Whether to enable verbose debug logging
106
+ */
107
+ debug?: boolean;
90
108
  }
91
109
  export interface NetDriver extends HybridObject<{
92
110
  ios: 'swift';
@@ -94,6 +112,7 @@ export interface NetDriver extends HybridObject<{
94
112
  }> {
95
113
  createSocket(id?: string): NetSocketDriver;
96
114
  createServer(): NetServerDriver;
115
+ createHttpParser(mode: number): HttpParser;
97
116
  createSecureContext(cert: string, key: string, passphrase?: string): number;
98
117
  createEmptySecureContext(): number;
99
118
  addCACertToSecureContext(scId: number, ca: string): void;
@@ -109,3 +128,4 @@ export interface NetDriver extends HybridObject<{
109
128
  */
110
129
  initWithConfig(config: NetConfig): void;
111
130
  }
131
+ //# sourceMappingURL=Net.nitro.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Net.nitro.d.ts","sourceRoot":"","sources":["../src/Net.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM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,MAAM,EAAE,SAAS,GAAG,IAAI,CAAA;CAC1C"}
package/lib/Net.nitro.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NetServerEvent = exports.NetSocketEvent = void 0;
4
- var NetSocketEvent;
1
+ export var NetSocketEvent;
5
2
  (function (NetSocketEvent) {
6
3
  NetSocketEvent[NetSocketEvent["CONNECT"] = 1] = "CONNECT";
7
4
  NetSocketEvent[NetSocketEvent["DATA"] = 2] = "DATA";
@@ -13,11 +10,11 @@ var NetSocketEvent;
13
10
  NetSocketEvent[NetSocketEvent["SESSION"] = 9] = "SESSION";
14
11
  NetSocketEvent[NetSocketEvent["KEYLOG"] = 10] = "KEYLOG";
15
12
  NetSocketEvent[NetSocketEvent["OCSP"] = 11] = "OCSP";
16
- })(NetSocketEvent || (exports.NetSocketEvent = NetSocketEvent = {}));
17
- var NetServerEvent;
13
+ })(NetSocketEvent || (NetSocketEvent = {}));
14
+ export var NetServerEvent;
18
15
  (function (NetServerEvent) {
19
16
  NetServerEvent[NetServerEvent["CONNECTION"] = 6] = "CONNECTION";
20
17
  NetServerEvent[NetServerEvent["ERROR"] = 3] = "ERROR";
21
18
  NetServerEvent[NetServerEvent["CLOSE"] = 4] = "CLOSE";
22
19
  NetServerEvent[NetServerEvent["DEBUG"] = 9] = "DEBUG";
23
- })(NetServerEvent || (exports.NetServerEvent = NetServerEvent = {}));
20
+ })(NetServerEvent || (NetServerEvent = {}));
package/lib/http.d.ts ADDED
@@ -0,0 +1,204 @@
1
+ import { Writable, Readable } from 'readable-stream';
2
+ import { EventEmitter } from 'eventemitter3';
3
+ import { Socket } from './net';
4
+ export declare const STATUS_CODES: Record<number, string>;
5
+ export declare const METHODS: string[];
6
+ export declare class IncomingMessage extends Readable {
7
+ httpVersion: string;
8
+ httpVersionMajor: number;
9
+ httpVersionMinor: number;
10
+ method?: string;
11
+ url?: string;
12
+ statusCode?: number;
13
+ statusMessage?: string;
14
+ headers: Record<string, string | string[]>;
15
+ rawHeaders: string[];
16
+ socket: Socket;
17
+ aborted: boolean;
18
+ complete: boolean;
19
+ trailers: Record<string, string>;
20
+ constructor(socket: Socket);
21
+ _read(): void;
22
+ setTimeout(msecs: number, callback?: () => void): this;
23
+ destroy(error?: Error): this;
24
+ setNoDelay(noDelay?: boolean): void;
25
+ setKeepAlive(enable?: boolean, initialDelay?: number): void;
26
+ }
27
+ export declare class OutgoingMessage extends Writable {
28
+ headersSent: boolean;
29
+ protected _headers: Record<string, any>;
30
+ protected _headerNames: Record<string, string>;
31
+ socket: Socket | null;
32
+ chunkedEncoding: boolean;
33
+ protected _hasBody: boolean;
34
+ protected _sendHeadersSent: boolean;
35
+ aborted: boolean;
36
+ protected _trailers: Record<string, string> | null;
37
+ constructor();
38
+ destroy(error?: Error): this;
39
+ setHeader(name: string, value: any): this;
40
+ getHeader(name: string): any;
41
+ removeHeader(name: string): void;
42
+ hasHeader(name: string): boolean;
43
+ getHeaderNames(): string[];
44
+ setTimeout(ms: number, callback?: () => void): this;
45
+ protected _renderHeaders(firstLine: string): string;
46
+ protected _sendHeaders(firstLine: string): void;
47
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
48
+ write(chunk: any, encoding?: any, callback?: any): boolean;
49
+ _final(callback: (error?: Error | null) => void): void;
50
+ addTrailers(headers: Record<string, string>): void;
51
+ end(chunk?: any, encoding?: any, callback?: any): this;
52
+ setNoDelay(noDelay?: boolean): void;
53
+ setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
54
+ }
55
+ export declare class ServerResponse extends OutgoingMessage {
56
+ statusCode: number;
57
+ statusMessage?: string;
58
+ socket: Socket;
59
+ constructor(socket: Socket);
60
+ writeHead(statusCode: number, statusMessage?: string | Record<string, any>, headers?: Record<string, any>): this;
61
+ private _sendResponseHeaders;
62
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
63
+ write(chunk: any, encoding?: any, callback?: any): boolean;
64
+ end(chunk?: any, encoding?: any, callback?: any): this;
65
+ }
66
+ export interface ServerOptions {
67
+ /**
68
+ * Optionally overrides all net.Server options.
69
+ */
70
+ IncomingMessage?: typeof IncomingMessage;
71
+ ServerResponse?: typeof ServerResponse;
72
+ /**
73
+ * Keep-Alive header timeout in milliseconds.
74
+ */
75
+ keepAliveTimeout?: number;
76
+ /**
77
+ * Request timeout in milliseconds.
78
+ */
79
+ requestTimeout?: number;
80
+ /**
81
+ * Headers timeout in milliseconds.
82
+ */
83
+ headersTimeout?: number;
84
+ /**
85
+ * Max header size in bytes.
86
+ */
87
+ maxHeaderSize?: number;
88
+ /**
89
+ * If defined, sets the maximum number of requests socket can handle.
90
+ */
91
+ maxRequestsPerSocket?: number;
92
+ }
93
+ export declare class Server extends EventEmitter {
94
+ protected _netServer: any;
95
+ protected _httpConnections: Set<Socket>;
96
+ maxHeaderSize: number;
97
+ maxRequestsPerSocket: number;
98
+ headersTimeout: number;
99
+ requestTimeout: number;
100
+ keepAliveTimeout: number;
101
+ constructor(options?: ServerOptions | ((req: IncomingMessage, res: ServerResponse) => void), requestListener?: (req: IncomingMessage, res: ServerResponse) => void);
102
+ protected _setupHttpConnection(socket: Socket): void;
103
+ listen(...args: any[]): this;
104
+ close(callback?: (err?: Error) => void): this;
105
+ [Symbol.asyncDispose](): Promise<void>;
106
+ address(): {
107
+ port: number;
108
+ family: string;
109
+ address: string;
110
+ } | null;
111
+ get listening(): boolean;
112
+ setTimeout(ms: number, callback?: () => void): this;
113
+ }
114
+ export interface AgentOptions {
115
+ keepAlive?: boolean;
116
+ keepAliveMsecs?: number;
117
+ maxSockets?: number;
118
+ maxTotalSockets?: number;
119
+ maxFreeSockets?: number;
120
+ scheduling?: 'fifo' | 'lifo';
121
+ timeout?: number;
122
+ maxCachedSessions?: number;
123
+ }
124
+ export declare class Agent extends EventEmitter {
125
+ maxSockets: number;
126
+ maxTotalSockets: number;
127
+ maxFreeSockets: number;
128
+ keepAlive: boolean;
129
+ keepAliveMsecs: number;
130
+ maxCachedSessions: number;
131
+ scheduling: 'fifo' | 'lifo';
132
+ requests: Record<string, ClientRequest[]>;
133
+ sockets: Record<string, Socket[]>;
134
+ freeSockets: Record<string, Socket[]>;
135
+ private _totalSockets;
136
+ proxy: string | null;
137
+ /**
138
+ * Gets the proxy URL for the given request options.
139
+ * Checks HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables.
140
+ *
141
+ * @param options Request options to determine if proxy should be used
142
+ * @returns Proxy URL or null if no proxy should be used
143
+ */
144
+ protected getProxy(options: RequestOptions): string | null;
145
+ constructor(options?: AgentOptions);
146
+ getName(options: RequestOptions): string;
147
+ addRequest(req: ClientRequest, options: RequestOptions): void;
148
+ createConnection(options: RequestOptions, callback: (err: Error | null, socket: Socket) => void): Socket;
149
+ releaseSocket(socket: Socket, options: RequestOptions): void;
150
+ keepSocketAlive(_socket: Socket): boolean;
151
+ reuseSocket(socket: Socket, req: ClientRequest): void;
152
+ private _removeSocket;
153
+ destroy(): void;
154
+ }
155
+ export declare const globalAgent: Agent;
156
+ export interface RequestOptions {
157
+ protocol?: string;
158
+ host?: string;
159
+ hostname?: string;
160
+ family?: number;
161
+ port?: number;
162
+ localAddress?: string;
163
+ socketPath?: string;
164
+ method?: string;
165
+ path?: string;
166
+ headers?: Record<string, any>;
167
+ auth?: string;
168
+ agent?: Agent | boolean;
169
+ timeout?: number;
170
+ rejectUnauthorized?: boolean;
171
+ }
172
+ export declare class ClientRequest extends OutgoingMessage {
173
+ method: string;
174
+ path: string;
175
+ host: string;
176
+ private _res?;
177
+ private _options;
178
+ private _connected;
179
+ private _pendingWrites;
180
+ private _ended;
181
+ private _expectContinue;
182
+ private _continueReceived;
183
+ constructor(options: RequestOptions, callback?: (res: IncomingMessage) => void);
184
+ /** @internal */
185
+ onSocket(socket: Socket | null): void;
186
+ private _connect;
187
+ private _attachSocketListeners;
188
+ private _socketCleanup?;
189
+ private _cleanupSocket;
190
+ private _finishResponse;
191
+ private _flushPendingWrites;
192
+ private _finishRequest;
193
+ private _sendRequest;
194
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
195
+ write(chunk: any, encoding?: any, callback?: any): boolean;
196
+ end(chunk?: any, encoding?: any, callback?: any): this;
197
+ abort(): void;
198
+ flushHeaders(): void;
199
+ }
200
+ export declare function createServer(requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server;
201
+ export declare function createServer(options: ServerOptions, requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server;
202
+ export declare function request(urlOrOptions: string | URL | RequestOptions, optionsOrCallback?: RequestOptions | ((res: IncomingMessage) => void), callback?: (res: IncomingMessage) => void): ClientRequest;
203
+ export declare function get(urlOrOptions: string | URL | RequestOptions, optionsOrCallback?: RequestOptions | ((res: IncomingMessage) => void), callback?: (res: IncomingMessage) => void): ClientRequest;
204
+ //# sourceMappingURL=http.d.ts.map