|
| 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"; |
0 commit comments