_meta.lua (168328B)
1 --- @meta 2 --- @class uv 3 local uv = {} 4 uv.constants = {} 5 6 --- # LibUV in Lua 7 --- 8 --- The [luv][] project provides access to the multi-platform support library 9 --- [libuv][] in Lua code. It was primarily developed for the [luvit][] project as 10 --- the built-in `uv` module, but can be used in other Lua environments. 11 --- 12 --- More information about the core libuv library can be found at the original 13 --- [libuv documentation page][]. 14 15 16 --- # TCP Echo Server Example 17 --- 18 --- Here is a small example showing a TCP echo server: 19 --- 20 --- ```lua 21 --- local uv = require("luv") -- "luv" when stand-alone, "uv" in luvi apps 22 --- 23 --- local server = uv.new_tcp() 24 --- server:bind("127.0.0.1", 1337) 25 --- server:listen(128, function (err) 26 --- assert(not err, err) 27 --- local client = uv.new_tcp() 28 --- server:accept(client) 29 --- client:read_start(function (err, chunk) 30 --- assert(not err, err) 31 --- if chunk then 32 --- client:write(chunk) 33 --- else 34 --- client:shutdown() 35 --- client:close() 36 --- end 37 --- end) 38 --- end) 39 --- print("TCP server listening at 127.0.0.1 port 1337") 40 --- uv.run() -- an explicit run call is necessary outside of luvit 41 --- ``` 42 43 --- # Module Layout 44 --- 45 --- The luv library contains a single Lua module referred to hereafter as `uv` for 46 --- simplicity. This module consists mostly of functions with names corresponding to 47 --- their original libuv versions. For example, the libuv function `uv_tcp_bind` has 48 --- a luv version at `uv.tcp_bind`. Currently, only two non-function fields exists: 49 --- `uv.constants` and `uv.errno`, which are tables. 50 51 --- # Functions vs Methods 52 --- 53 --- In addition to having simple functions, luv provides an optional method-style 54 --- API. For example, `uv.tcp_bind(server, host, port)` can alternatively be called 55 --- as `server:bind(host, port)`. Note that the first argument `server` becomes the 56 --- object and `tcp_` is removed from the function name. Method forms are 57 --- documented below where they exist. 58 59 --- # Synchronous vs Asynchronous Functions 60 --- 61 --- Functions that accept a callback are asynchronous. These functions may 62 --- immediately return results to the caller to indicate their initial status, but 63 --- their final execution is deferred until at least the next libuv loop iteration. 64 --- After completion, their callbacks are executed with any results passed to it. 65 --- 66 --- Functions that do not accept a callback are synchronous. These functions 67 --- immediately return their results to the caller. 68 --- 69 --- Some (generally FS and DNS) functions can behave either synchronously or 70 --- asynchronously. If a callback is provided to these functions, they behave 71 --- asynchronously; if no callback is provided, they behave synchronously. 72 73 --- # Pseudo-Types 74 --- 75 --- Some unique types are defined. These are not actual types in Lua, but they are 76 --- used here to facilitate documenting consistent behavior: 77 --- - `fail`: an assertable `nil, string, string` tuple (see [Error Handling][]) 78 --- - `callable`: a `function`; or a `table` or `userdata` with a `__call` 79 --- metamethod 80 --- - `buffer`: a `string` or a sequential `table` of `string`s 81 --- - `threadargs`: variable arguments (`...`) of type `nil`, `boolean`, `number`, 82 --- `string`, or `userdata`, numbers of argument limited to 9. 83 84 85 --- # Contents 86 --- 87 --- This documentation is mostly a retelling of the [libuv API documentation][] 88 --- within the context of luv's Lua API. Low-level implementation details and 89 --- unexposed C functions and types are not documented here except for when they 90 --- are relevant to behavior seen in the Lua module. 91 --- 92 --- - [Constants][] 93 --- - [Error Handling][] 94 --- - [Version Checking][] 95 --- - [`uv_loop_t`][] — Event loop 96 --- - [`uv_req_t`][] — Base request 97 --- - [`uv_handle_t`][] — Base handle 98 --- - [`uv_timer_t`][] — Timer handle 99 --- - [`uv_prepare_t`][] — Prepare handle 100 --- - [`uv_check_t`][] — Check handle 101 --- - [`uv_idle_t`][] — Idle handle 102 --- - [`uv_async_t`][] — Async handle 103 --- - [`uv_poll_t`][] — Poll handle 104 --- - [`uv_signal_t`][] — Signal handle 105 --- - [`uv_process_t`][] — Process handle 106 --- - [`uv_stream_t`][] — Stream handle 107 --- - [`uv_tcp_t`][] — TCP handle 108 --- - [`uv_pipe_t`][] — Pipe handle 109 --- - [`uv_tty_t`][] — TTY handle 110 --- - [`uv_udp_t`][] — UDP handle 111 --- - [`uv_fs_event_t`][] — FS Event handle 112 --- - [`uv_fs_poll_t`][] — FS Poll handle 113 --- - [File system operations][] 114 --- - [Thread pool work scheduling][] 115 --- - [DNS utility functions][] 116 --- - [Threading and synchronization utilities][] 117 --- - [Miscellaneous utilities][] 118 --- - [Metrics operations][] 119 120 --- # Constants 121 --- 122 --- As a Lua library, luv supports and encourages the use of lowercase strings to 123 --- represent options. For example: 124 --- ```lua 125 --- -- signal start with string input 126 --- uv.signal_start("sigterm", function(signame) 127 --- print(signame) -- string output: "sigterm" 128 --- end) 129 --- ``` 130 --- 131 --- However, luv also superficially exposes libuv constants in a Lua table at 132 --- `uv.constants` where its keys are uppercase constant names and their associated 133 --- values are integers defined internally by libuv. The values from this table may 134 --- be supported as function arguments, but their use may not change the output 135 --- type. For example: 136 --- 137 --- ```lua 138 --- -- signal start with integer input 139 --- uv.signal_start(uv.constants.SIGTERM, function(signame) 140 --- print(signame) -- string output: "sigterm" 141 --- end) 142 --- ``` 143 --- 144 --- The uppercase constants defined in `uv.constants` that have associated 145 --- lowercase option strings are listed below. 146 147 --- # Address Families 148 uv.constants.AF_UNIX = 'unix' 149 uv.constants.AF_INET = 'inet' 150 uv.constants.AF_INET6 = 'inet6' 151 uv.constants.AF_IPX = 'ipx' 152 uv.constants.AF_NETLINK = 'netlink' 153 uv.constants.AF_X25 = 'x25' 154 uv.constants.AF_AX25 = 'as25' 155 uv.constants.AF_ATMPVC = 'atmpvc' 156 uv.constants.AF_APPLETALK = 'appletalk' 157 uv.constants.AF_PACKET = 'packet' 158 159 --- # Signals 160 uv.constants.SIGHUP = 'sighup' 161 uv.constants.SIGINT = 'sigint' 162 uv.constants.SIGQUIT = 'sigquit' 163 uv.constants.SIGILL = 'sigill' 164 uv.constants.SIGTRAP = 'sigtrap' 165 uv.constants.SIGABRT = 'sigabrt' 166 uv.constants.SIGIOT = 'sigiot' 167 uv.constants.SIGBUS = 'sigbus' 168 uv.constants.SIGFPE = 'sigfpe' 169 uv.constants.SIGKILL = 'sigkill' 170 uv.constants.SIGUSR1 = 'sigusr1' 171 uv.constants.SIGSEGV = 'sigsegv' 172 uv.constants.SIGUSR2 = 'sigusr2' 173 uv.constants.SIGPIPE = 'sigpipe' 174 uv.constants.SIGALRM = 'sigalrm' 175 uv.constants.SIGTERM = 'sigterm' 176 uv.constants.SIGCHLD = 'sigchld' 177 uv.constants.SIGSTKFLT = 'sigstkflt' 178 uv.constants.SIGCONT = 'sigcont' 179 uv.constants.SIGSTOP = 'sigstop' 180 uv.constants.SIGTSTP = 'sigtstp' 181 uv.constants.SIGBREAK = 'sigbreak' 182 uv.constants.SIGTTIN = 'sigttin' 183 uv.constants.SIGTTOU = 'sigttou' 184 uv.constants.SIGURG = 'sigurg' 185 uv.constants.SIGXCPU = 'sigxcpu' 186 uv.constants.SIGXFSZ = 'sigxfsz' 187 uv.constants.SIGVTALRM = 'sigvtalrm' 188 uv.constants.SIGPROF = 'sigprof' 189 uv.constants.SIGWINCH = 'sigwinch' 190 uv.constants.SIGIO = 'sigio' 191 uv.constants.SIGPOLL = 'sigpoll' 192 uv.constants.SIGLOST = 'siglost' 193 uv.constants.SIGPWR = 'sigpwr' 194 uv.constants.SIGSYS = 'sigsys' 195 196 --- # Socket Types 197 uv.constants.SOCK_STREAM = 'stream' 198 uv.constants.SOCK_DGRAM = 'dgram' 199 uv.constants.SOCK_SEQPACKET = 'seqpacket' 200 uv.constants.SOCK_RAW = 'raw' 201 uv.constants.SOCK_RDM = 'rdm' 202 203 --- # TTY Modes 204 uv.constants.TTY_MODE_NORMAL = 'normal' 205 uv.constants.TTY_MODE_RAW = 'raw' 206 uv.constants.TTY_MODE_IO = 'io' 207 uv.constants.TTY_MODE_RAW_VT = 'raw_vt' 208 209 --- # FS Modification Times 210 uv.constants.FS_UTIME_NOW = 'now' 211 uv.constants.FS_UTIME_OMIT = 'omit' 212 213 214 --- # Error Handling 215 --- 216 --- In libuv, errors are represented by negative numbered constants. While these 217 --- constants are made available in the `uv.errno` table, they are not returned by 218 --- luv functions and the libuv functions used to handle them are not exposed. 219 --- Instead, if an internal error is encountered, the failing luv function will 220 --- return to the caller an assertable `nil, err, name` tuple: 221 --- 222 --- - `nil` idiomatically indicates failure 223 --- - `err` is a string with the format `{name}: {message}` 224 --- - `{name}` is the error name provided internally by `uv_err_name` 225 --- - `{message}` is a human-readable message provided internally by `uv_strerror` 226 --- - `name` is the same string used to construct `err` 227 --- 228 --- This tuple is referred to below as the `fail` pseudo-type. 229 --- 230 --- When a function is called successfully, it will return either a value that is 231 --- relevant to the operation of the function, or the integer `0` to indicate 232 --- success, or sometimes nothing at all. These cases are documented below. 233 --- 234 --- Below is a list of known error names and error strings. See libuv's 235 --- [error constants][] page for an original source. 236 237 --- @alias uv.error_name 238 --- | 'E2BIG' # argument list too long. 239 --- | 'EACCES' # permission denied. 240 --- | 'EADDRINUSE' # address already in use. 241 --- | 'EADDRNOTAVAIL' # address not available. 242 --- | 'EAFNOSUPPORT' # address family not supported. 243 --- | 'EAGAIN' # resource temporarily unavailable. 244 --- | 'EAI_ADDRFAMILY' # address family not supported. 245 --- | 'EAI_AGAIN' # temporary failure. 246 --- | 'EAI_BADFLAGS' # bad ai_flags value. 247 --- | 'EAI_BADHINTS' # invalid value for hints. 248 --- | 'EAI_CANCELED' # request canceled. 249 --- | 'EAI_FAIL' # permanent failure. 250 --- | 'EAI_FAMILY' # ai_family not supported. 251 --- | 'EAI_MEMORY' # out of memory. 252 --- | 'EAI_NODATA' # no address. 253 --- | 'EAI_NONAME' # unknown node or service. 254 --- | 'EAI_OVERFLOW' # argument buffer overflow. 255 --- | 'EAI_PROTOCOL' # resolved protocol is unknown. 256 --- | 'EAI_SERVICE' # service not available for socket type. 257 --- | 'EAI_SOCKTYPE' # socket type not supported. 258 --- | 'EALREADY' # connection already in progress. 259 --- | 'EBADF' # bad file descriptor. 260 --- | 'EBUSY' # resource busy or locked. 261 --- | 'ECANCELED' # operation canceled. 262 --- | 'ECHARSET' # invalid Unicode character. 263 --- | 'ECONNABORTED' # software caused connection abort. 264 --- | 'ECONNREFUSED' # connection refused. 265 --- | 'ECONNRESET' # connection reset by peer. 266 --- | 'EDESTADDRREQ' # destination address required. 267 --- | 'EEXIST' # file already exists. 268 --- | 'EFAULT' # bad address in system call argument. 269 --- | 'EFBIG' # file too large. 270 --- | 'EHOSTUNREACH' # host is unreachable. 271 --- | 'EINTR' # interrupted system call. 272 --- | 'EINVAL' # invalid argument. 273 --- | 'EIO' # i/o error. 274 --- | 'EISCONN' # socket is already connected. 275 --- | 'EISDIR' # illegal operation on a directory. 276 --- | 'ELOOP' # too many symbolic links encountered. 277 --- | 'EMFILE' # too many open files. 278 --- | 'EMSGSIZE' # message too long. 279 --- | 'ENAMETOOLONG' # name too long. 280 --- | 'ENETDOWN' # network is down. 281 --- | 'ENETUNREACH' # network is unreachable. 282 --- | 'ENFILE' # file table overflow. 283 --- | 'ENOBUFS' # no buffer space available. 284 --- | 'ENODEV' # no such device. 285 --- | 'ENOENT' # no such file or directory. 286 --- | 'ENOMEM' # not enough memory. 287 --- | 'ENONET' # machine is not on the network. 288 --- | 'ENOPROTOOPT' # protocol not available. 289 --- | 'ENOSPC' # no space left on device. 290 --- | 'ENOSYS' # function not implemented. 291 --- | 'ENOTCONN' # socket is not connected. 292 --- | 'ENOTDIR' # not a directory. 293 --- | 'ENOTEMPTY' # directory not empty. 294 --- | 'ENOTSOCK' # socket operation on non-socket. 295 --- | 'ENOTSUP' # operation not supported on socket. 296 --- | 'EOVERFLOW' # value too large for defined data type. 297 --- | 'EPERM' # operation not permitted. 298 --- | 'EPIPE' # broken pipe. 299 --- | 'EPROTO' # protocol error. 300 --- | 'EPROTONOSUPPORT' # protocol not supported. 301 --- | 'EPROTOTYPE' # protocol wrong type for socket. 302 --- | 'ERANGE' # result too large. 303 --- | 'EROFS' # read-only file system. 304 --- | 'ESHUTDOWN' # cannot send after transport endpoint shutdown. 305 --- | 'ESPIPE' # invalid seek. 306 --- | 'ESRCH' # no such process. 307 --- | 'ETIMEDOUT' # connection timed out. 308 --- | 'ETXTBSY' # text file is busy. 309 --- | 'EXDEV' # cross-device link not permitted. 310 --- | 'UNKNOWN' # unknown error. 311 --- | 'EOF' # end of file. 312 --- | 'ENXIO' # no such device or address. 313 --- | 'EMLINK' # too many links. 314 --- | 'ENOTTY' # inappropriate ioctl for device. 315 --- | 'EFTYPE' # inappropriate file type or format. 316 --- | 'EILSEQ' # illegal byte sequence. 317 --- | 'ESOCKTNOSUPPORT' # socket type not supported. 318 319 --- # Version Checking 320 321 --- Returns the libuv version packed into a single integer. 8 bits are used for each 322 --- component, with the patch number stored in the 8 least significant bits. For 323 --- example, this would be 0x010203 in libuv 1.2.3. 324 --- @return integer 325 function uv.version() end 326 327 --- Returns the libuv version number as a string. For example, this would be "1.2.3" 328 --- in libuv 1.2.3. For non-release versions, the version suffix is included. 329 --- @return string 330 function uv.version_string() end 331 332 333 --- # `uv_loop_t` - Event loop 334 --- 335 --- The event loop is the central part of libuv's functionality. It takes care of 336 --- polling for I/O and scheduling callbacks to be run based on different sources of 337 --- events. 338 --- 339 --- In luv, there is an implicit uv loop for every Lua state that loads the library. 340 --- You can use this library in an multi-threaded environment as long as each thread 341 --- has it's own Lua state with its corresponding own uv loop. This loop is not 342 --- directly exposed to users in the Lua module. 343 344 --- Closes all internal loop resources. In normal execution, the loop will 345 --- automatically be closed when it is garbage collected by Lua, so it is not 346 --- necessary to explicitly call `loop_close()`. Call this function only after the 347 --- loop has finished executing and all open handles and requests have been closed, 348 --- or it will return `EBUSY`. 349 --- @return 0? success 350 --- @return string? err 351 --- @return uv.error_name? err_name 352 function uv.loop_close() end 353 354 --- This function runs the event loop. It will act differently depending on the 355 --- specified mode: 356 --- 357 --- - `"default"`: Runs the event loop until there are no more active and 358 --- referenced handles or requests. Returns `true` if `uv.stop()` was called and 359 --- there are still active handles or requests. Returns `false` in all other 360 --- cases. 361 --- 362 --- - `"once"`: Poll for I/O once. Note that this function blocks if there are no 363 --- pending callbacks. Returns `false` when done (no active handles or requests 364 --- left), or `true` if more callbacks are expected (meaning you should run the 365 --- event loop again sometime in the future). 366 --- 367 --- - `"nowait"`: Poll for I/O once but don't block if there are no pending 368 --- callbacks. Returns `false` if done (no active handles or requests left), 369 --- or `true` if more callbacks are expected (meaning you should run the event 370 --- loop again sometime in the future). 371 --- **Note**: 372 --- Luvit will implicitly call `uv.run()` after loading user code, but if 373 --- you use the luv bindings directly, you need to call this after registering 374 --- your initial set of event callbacks to start the event loop. 375 --- @param mode string? 376 --- @return boolean? running 377 --- @return string? err 378 --- @return uv.error_name? err_name 379 function uv.run(mode) end 380 381 --- Set additional loop options. You should normally call this before the first call 382 --- to uv_run() unless mentioned otherwise. 383 --- 384 --- Supported options: 385 --- 386 --- - `"block_signal"`: Block a signal when polling for new events. The second argument 387 --- to loop_configure() is the signal name (as a lowercase string) or the signal number. 388 --- This operation is currently only implemented for `"sigprof"` signals, to suppress 389 --- unnecessary wakeups when using a sampling profiler. Requesting other signals will 390 --- fail with `EINVAL`. 391 --- - `"metrics_idle_time"`: Accumulate the amount of idle time the event loop spends 392 --- in the event provider. This option is necessary to use `metrics_idle_time()`. 393 --- 394 --- An example of a valid call to this function is: 395 --- 396 --- ```lua 397 --- uv.loop_configure("block_signal", "sigprof") 398 --- ``` 399 --- **Note**: 400 --- Be prepared to handle the `ENOSYS` error; it means the loop option is 401 --- not supported by the platform. 402 --- @param option string 403 --- @param ... any depends on `option` 404 --- @return 0? success 405 --- @return string? err 406 --- @return uv.error_name? err_name 407 function uv.loop_configure(option, ...) end 408 409 --- If the loop is running, returns a string indicating the mode in use. If the loop 410 --- is not running, `nil` is returned instead. 411 --- @return string? 412 function uv.loop_mode() end 413 414 --- Returns `true` if there are referenced active handles, active requests, or 415 --- closing handles in the loop; otherwise, `false`. 416 --- @return boolean? alive 417 --- @return string? err 418 --- @return uv.error_name? err_name 419 function uv.loop_alive() end 420 421 --- Stop the event loop, causing `uv.run()` to end as soon as possible. This 422 --- will happen not sooner than the next loop iteration. If this function was called 423 --- before blocking for I/O, the loop won't block for I/O on this iteration. 424 function uv.stop() end 425 426 --- Get backend file descriptor. Only kqueue, epoll, and event ports are supported. 427 --- 428 --- This can be used in conjunction with `uv.run("nowait")` to poll in one thread 429 --- and run the event loop's callbacks in another 430 --- **Note**: 431 --- Embedding a kqueue fd in another kqueue pollset doesn't work on all 432 --- platforms. It's not an error to add the fd but it never generates events. 433 --- @return integer? 434 function uv.backend_fd() end 435 436 --- Get the poll timeout. The return value is in milliseconds, or -1 for no timeout. 437 --- @return integer 438 function uv.backend_timeout() end 439 440 --- Returns the current timestamp in milliseconds. The timestamp is cached at the 441 --- start of the event loop tick, see `uv.update_time()` for details and rationale. 442 --- 443 --- The timestamp increases monotonically from some arbitrary point in time. Don't 444 --- make assumptions about the starting point, you will only get disappointed. 445 --- **Note**: 446 --- Use `uv.hrtime()` if you need sub-millisecond granularity. 447 --- @return integer 448 function uv.now() end 449 450 --- Update the event loop's concept of "now". Libuv caches the current time at the 451 --- start of the event loop tick in order to reduce the number of time-related 452 --- system calls. 453 --- 454 --- You won't normally need to call this function unless you have callbacks that 455 --- block the event loop for longer periods of time, where "longer" is somewhat 456 --- subjective but probably on the order of a millisecond or more. 457 function uv.update_time() end 458 459 --- Walk the list of handles: `callback` will be executed with each handle. 460 --- Example 461 --- ```lua 462 --- -- Example usage of uv.walk to close all handles that aren't already closing. 463 --- uv.walk(function (handle) 464 --- if not handle:is_closing() then 465 --- handle:close() 466 --- end 467 --- end) 468 --- ``` 469 --- @param callback fun(handle: uv.uv_handle_t) 470 function uv.walk(callback) end 471 472 473 --- # `uv_req_t` - Base request 474 --- 475 --- `uv_req_t` is the base type for all libuv request types. 476 --- @class uv.uv_req_t : userdata 477 local uv_req_t = {} 478 479 --- Cancel a pending request. Fails if the request is executing or has finished 480 --- executing. Only cancellation of `uv_fs_t`, `uv_getaddrinfo_t`, 481 --- `uv_getnameinfo_t` and `uv_work_t` requests is currently supported. 482 --- @param req uv.uv_req_t 483 --- @return 0? success 484 --- @return string? err 485 --- @return uv.error_name? err_name 486 function uv.cancel(req) end 487 488 --- Cancel a pending request. Fails if the request is executing or has finished 489 --- executing. Only cancellation of `uv_fs_t`, `uv_getaddrinfo_t`, 490 --- `uv_getnameinfo_t` and `uv_work_t` requests is currently supported. 491 --- @return 0? success 492 --- @return string? err 493 --- @return uv.error_name? err_name 494 function uv_req_t:cancel() end 495 496 --- Returns the name of the struct for a given request (e.g. `"fs"` for `uv_fs_t`) 497 --- and the libuv enum integer for the request's type (`uv_req_type`). 498 --- @param req uv.uv_req_t 499 --- @return string type 500 --- @return integer enum 501 function uv.req_get_type(req) end 502 503 --- Returns the name of the struct for a given request (e.g. `"fs"` for `uv_fs_t`) 504 --- and the libuv enum integer for the request's type (`uv_req_type`). 505 --- @return string type 506 --- @return integer enum 507 function uv_req_t:get_type() end 508 509 510 --- # `uv_handle_t` - Base handle 511 --- 512 --- `uv_handle_t` is the base type for all libuv handle types. All API functions 513 --- defined here work with any handle type. 514 --- @class uv.uv_handle_t : userdata 515 local uv_handle_t = {} 516 517 --- Returns `true` if the handle is active, `false` if it's inactive. What "active” 518 --- means depends on the type of handle: 519 --- 520 --- - A [`uv_async_t`][] handle is always active and cannot be deactivated, except 521 --- by closing it with `uv.close()`. 522 --- 523 --- - A [`uv_pipe_t`][], [`uv_tcp_t`][], [`uv_udp_t`][], etc. handle - basically 524 --- any handle that deals with I/O - is active when it is doing something that 525 --- involves I/O, like reading, writing, connecting, accepting new connections, 526 --- etc. 527 --- 528 --- - A [`uv_check_t`][], [`uv_idle_t`][], [`uv_timer_t`][], etc. handle is active 529 --- when it has been started with a call to `uv.check_start()`, `uv.idle_start()`, 530 --- `uv.timer_start()` etc. until it has been stopped with a call to its 531 --- respective stop function. 532 --- @param handle uv.uv_handle_t 533 --- @return boolean? active 534 --- @return string? err 535 --- @return uv.error_name? err_name 536 function uv.is_active(handle) end 537 538 --- Returns `true` if the handle is active, `false` if it's inactive. What "active” 539 --- means depends on the type of handle: 540 --- 541 --- - A [`uv_async_t`][] handle is always active and cannot be deactivated, except 542 --- by closing it with `uv.close()`. 543 --- 544 --- - A [`uv_pipe_t`][], [`uv_tcp_t`][], [`uv_udp_t`][], etc. handle - basically 545 --- any handle that deals with I/O - is active when it is doing something that 546 --- involves I/O, like reading, writing, connecting, accepting new connections, 547 --- etc. 548 --- 549 --- - A [`uv_check_t`][], [`uv_idle_t`][], [`uv_timer_t`][], etc. handle is active 550 --- when it has been started with a call to `uv.check_start()`, `uv.idle_start()`, 551 --- `uv.timer_start()` etc. until it has been stopped with a call to its 552 --- respective stop function. 553 --- @return boolean? active 554 --- @return string? err 555 --- @return uv.error_name? err_name 556 function uv_handle_t:is_active() end 557 558 --- Returns `true` if the handle is closing or closed, `false` otherwise. 559 --- **Note**: 560 --- This function should only be used between the initialization of the 561 --- handle and the arrival of the close callback. 562 --- @param handle uv.uv_handle_t 563 --- @return boolean? closing 564 --- @return string? err 565 --- @return uv.error_name? err_name 566 function uv.is_closing(handle) end 567 568 --- Returns `true` if the handle is closing or closed, `false` otherwise. 569 --- **Note**: 570 --- This function should only be used between the initialization of the 571 --- handle and the arrival of the close callback. 572 --- @return boolean? closing 573 --- @return string? err 574 --- @return uv.error_name? err_name 575 function uv_handle_t:is_closing() end 576 577 --- Request handle to be closed. `callback` will be called asynchronously after this 578 --- call. This MUST be called on each handle before memory is released. 579 --- 580 --- Handles that wrap file descriptors are closed immediately but `callback` will 581 --- still be deferred to the next iteration of the event loop. It gives you a chance 582 --- to free up any resources associated with the handle. 583 --- 584 --- In-progress requests, like `uv_connect_t` or `uv_write_t`, are cancelled and 585 --- have their callbacks called asynchronously with `ECANCELED`. 586 --- @param handle uv.uv_handle_t 587 --- @param callback fun()? 588 function uv.close(handle, callback) end 589 590 --- Request handle to be closed. `callback` will be called asynchronously after this 591 --- call. This MUST be called on each handle before memory is released. 592 --- 593 --- Handles that wrap file descriptors are closed immediately but `callback` will 594 --- still be deferred to the next iteration of the event loop. It gives you a chance 595 --- to free up any resources associated with the handle. 596 --- 597 --- In-progress requests, like `uv_connect_t` or `uv_write_t`, are cancelled and 598 --- have their callbacks called asynchronously with `ECANCELED`. 599 --- @param callback fun()? 600 function uv_handle_t:close(callback) end 601 602 --- Reference the given handle. References are idempotent, that is, if a handle is 603 --- already referenced calling this function again will have no effect. 604 --- @param handle uv.uv_handle_t 605 function uv.ref(handle) end 606 607 --- Reference the given handle. References are idempotent, that is, if a handle is 608 --- already referenced calling this function again will have no effect. 609 function uv_handle_t:ref() end 610 611 --- Un-reference the given handle. References are idempotent, that is, if a handle 612 --- is not referenced calling this function again will have no effect. 613 --- @param handle uv.uv_handle_t 614 function uv.unref(handle) end 615 616 --- Un-reference the given handle. References are idempotent, that is, if a handle 617 --- is not referenced calling this function again will have no effect. 618 function uv_handle_t:unref() end 619 620 --- Returns `true` if the handle referenced, `false` if not. 621 --- @param handle uv.uv_handle_t 622 --- @return boolean? has_ref 623 --- @return string? err 624 --- @return uv.error_name? err_name 625 function uv.has_ref(handle) end 626 627 --- Returns `true` if the handle referenced, `false` if not. 628 --- @return boolean? has_ref 629 --- @return string? err 630 --- @return uv.error_name? err_name 631 function uv_handle_t:has_ref() end 632 633 --- Gets or sets the size of the send buffer that the operating system uses for the 634 --- socket. 635 --- 636 --- If `size` is omitted (or `0`), this will return the current send buffer size; otherwise, this will use `size` to set the new send buffer size. 637 --- 638 --- This function works for TCP, pipe and UDP handles on Unix and for TCP and UDP 639 --- handles on Windows. 640 --- **Note**: 641 --- Linux will set double the size and return double the size of the 642 --- original set value. 643 --- @param handle uv.uv_handle_t 644 --- @param size integer? 645 --- @return integer? success 646 --- @return string? err 647 --- @return uv.error_name? err_name 648 function uv.send_buffer_size(handle, size) end 649 650 --- Gets or sets the size of the send buffer that the operating system uses for the 651 --- socket. 652 --- 653 --- If `size` is omitted (or `0`), this will return the current send buffer size; otherwise, this will use `size` to set the new send buffer size. 654 --- 655 --- This function works for TCP, pipe and UDP handles on Unix and for TCP and UDP 656 --- handles on Windows. 657 --- **Note**: 658 --- Linux will set double the size and return double the size of the 659 --- original set value. 660 --- @param size integer? 661 --- @return integer? success 662 --- @return string? err 663 --- @return uv.error_name? err_name 664 function uv_handle_t:send_buffer_size(size) end 665 666 --- Gets or sets the size of the receive buffer that the operating system uses for 667 --- the socket. 668 --- 669 --- If `size` is omitted (or `0`), this will return the current send buffer size; otherwise, this will use `size` to set the new send buffer size. 670 --- 671 --- This function works for TCP, pipe and UDP handles on Unix and for TCP and UDP 672 --- handles on Windows. 673 --- **Note**: 674 --- Linux will set double the size and return double the size of the 675 --- original set value. 676 --- @param handle uv.uv_handle_t 677 --- @param size integer? 678 --- @return integer? success 679 --- @return string? err 680 --- @return uv.error_name? err_name 681 function uv.recv_buffer_size(handle, size) end 682 683 --- Gets or sets the size of the receive buffer that the operating system uses for 684 --- the socket. 685 --- 686 --- If `size` is omitted (or `0`), this will return the current send buffer size; otherwise, this will use `size` to set the new send buffer size. 687 --- 688 --- This function works for TCP, pipe and UDP handles on Unix and for TCP and UDP 689 --- handles on Windows. 690 --- **Note**: 691 --- Linux will set double the size and return double the size of the 692 --- original set value. 693 --- @param size integer? 694 --- @return integer? success 695 --- @return string? err 696 --- @return uv.error_name? err_name 697 function uv_handle_t:recv_buffer_size(size) end 698 699 --- Gets the platform dependent file descriptor equivalent. 700 --- 701 --- The following handles are supported: TCP, pipes, TTY, UDP and poll. Passing any 702 --- other handle type will fail with `EINVAL`. 703 --- 704 --- If a handle doesn't have an attached file descriptor yet or the handle itself 705 --- has been closed, this function will return `EBADF`. 706 --- **Warning**: 707 --- Be very careful when using this function. libuv assumes it's in 708 --- control of the file descriptor so any change to it may lead to malfunction. 709 --- @param handle uv.uv_handle_t 710 --- @return integer? fileno 711 --- @return string? err 712 --- @return uv.error_name? err_name 713 function uv.fileno(handle) end 714 715 --- Gets the platform dependent file descriptor equivalent. 716 --- 717 --- The following handles are supported: TCP, pipes, TTY, UDP and poll. Passing any 718 --- other handle type will fail with `EINVAL`. 719 --- 720 --- If a handle doesn't have an attached file descriptor yet or the handle itself 721 --- has been closed, this function will return `EBADF`. 722 --- **Warning**: 723 --- Be very careful when using this function. libuv assumes it's in 724 --- control of the file descriptor so any change to it may lead to malfunction. 725 --- @return integer? fileno 726 --- @return string? err 727 --- @return uv.error_name? err_name 728 function uv_handle_t:fileno() end 729 730 --- Returns the name of the struct for a given handle (e.g. `"pipe"` for `uv_pipe_t`) 731 --- and the libuv enum integer for the handle's type (`uv_handle_type`). 732 --- @param handle uv.uv_handle_t 733 --- @return string type 734 --- @return integer enum 735 function uv.handle_get_type(handle) end 736 737 --- Returns the name of the struct for a given handle (e.g. `"pipe"` for `uv_pipe_t`) 738 --- and the libuv enum integer for the handle's type (`uv_handle_type`). 739 --- @return string type 740 --- @return integer enum 741 function uv_handle_t:get_type() end 742 743 744 --- # Reference counting 745 --- 746 --- The libuv event loop (if run in the default mode) will run until there are no 747 --- active and referenced handles left. The user can force the loop to exit early by 748 --- unreferencing handles which are active, for example by calling `uv.unref()` 749 --- after calling `uv.timer_start()`. 750 --- 751 --- A handle can be referenced or unreferenced, the refcounting scheme doesn't use a 752 --- counter, so both operations are idempotent. 753 --- 754 --- All handles are referenced when active by default, see `uv.is_active()` for a 755 --- more detailed explanation on what being active involves. 756 757 --- # `uv_timer_t` - Timer handle 758 --- 759 --- > [`uv_handle_t`][] functions also apply. 760 --- 761 --- Timer handles are used to schedule callbacks to be called in the future. 762 --- @class uv.uv_timer_t : uv.uv_handle_t 763 local uv_timer_t = {} 764 765 --- Creates and initializes a new `uv_timer_t`. Returns the Lua userdata wrapping 766 --- it. 767 --- Example 768 --- ```lua 769 --- -- Creating a simple setTimeout wrapper 770 --- local function setTimeout(timeout, callback) 771 --- local timer = uv.new_timer() 772 --- timer:start(timeout, 0, function () 773 --- timer:stop() 774 --- timer:close() 775 --- callback() 776 --- end) 777 --- return timer 778 --- end 779 --- 780 --- -- Creating a simple setInterval wrapper 781 --- local function setInterval(interval, callback) 782 --- local timer = uv.new_timer() 783 --- timer:start(interval, interval, function () 784 --- callback() 785 --- end) 786 --- return timer 787 --- end 788 --- 789 --- -- And clearInterval 790 --- local function clearInterval(timer) 791 --- timer:stop() 792 --- timer:close() 793 --- end 794 --- ``` 795 --- @return uv.uv_timer_t? timer 796 --- @return string? err 797 --- @return uv.error_name? err_name 798 function uv.new_timer() end 799 800 --- Start the timer. `timeout` and `repeat` are in milliseconds. 801 --- 802 --- If `timeout` is zero, the callback fires on the next event loop iteration. If 803 --- `repeat` is non-zero, the callback fires first after `timeout` milliseconds and 804 --- then repeatedly after `repeat` milliseconds. 805 --- @param timer uv.uv_timer_t 806 --- @param timeout integer 807 --- @param repeat_ integer 808 --- @param callback fun() 809 --- @return 0? success 810 --- @return string? err 811 --- @return uv.error_name? err_name 812 function uv.timer_start(timer, timeout, repeat_, callback) end 813 814 --- Start the timer. `timeout` and `repeat` are in milliseconds. 815 --- 816 --- If `timeout` is zero, the callback fires on the next event loop iteration. If 817 --- `repeat` is non-zero, the callback fires first after `timeout` milliseconds and 818 --- then repeatedly after `repeat` milliseconds. 819 --- @param timeout integer 820 --- @param repeat_ integer 821 --- @param callback fun() 822 --- @return 0? success 823 --- @return string? err 824 --- @return uv.error_name? err_name 825 function uv_timer_t:start(timeout, repeat_, callback) end 826 827 --- Stop the timer, the callback will not be called anymore. 828 --- @param timer uv.uv_timer_t 829 --- @return 0? success 830 --- @return string? err 831 --- @return uv.error_name? err_name 832 function uv.timer_stop(timer) end 833 834 --- Stop the timer, the callback will not be called anymore. 835 --- @return 0? success 836 --- @return string? err 837 --- @return uv.error_name? err_name 838 function uv_timer_t:stop() end 839 840 --- Stop the timer, and if it is repeating restart it using the repeat value as the 841 --- timeout. If the timer has never been started before it raises `EINVAL`. 842 --- @param timer uv.uv_timer_t 843 --- @return 0? success 844 --- @return string? err 845 --- @return uv.error_name? err_name 846 function uv.timer_again(timer) end 847 848 --- Stop the timer, and if it is repeating restart it using the repeat value as the 849 --- timeout. If the timer has never been started before it raises `EINVAL`. 850 --- @return 0? success 851 --- @return string? err 852 --- @return uv.error_name? err_name 853 function uv_timer_t:again() end 854 855 --- Set the repeat interval value in milliseconds. The timer will be scheduled to 856 --- run on the given interval, regardless of the callback execution duration, and 857 --- will follow normal timer semantics in the case of a time-slice overrun. 858 --- 859 --- For example, if a 50 ms repeating timer first runs for 17 ms, it will be 860 --- scheduled to run again 33 ms later. If other tasks consume more than the 33 ms 861 --- following the first timer callback, then the callback will run as soon as 862 --- possible. 863 --- @param timer uv.uv_timer_t 864 --- @param repeat_ integer 865 function uv.timer_set_repeat(timer, repeat_) end 866 867 --- Set the repeat interval value in milliseconds. The timer will be scheduled to 868 --- run on the given interval, regardless of the callback execution duration, and 869 --- will follow normal timer semantics in the case of a time-slice overrun. 870 --- 871 --- For example, if a 50 ms repeating timer first runs for 17 ms, it will be 872 --- scheduled to run again 33 ms later. If other tasks consume more than the 33 ms 873 --- following the first timer callback, then the callback will run as soon as 874 --- possible. 875 --- @param repeat_ integer 876 function uv_timer_t:set_repeat(repeat_) end 877 878 --- Get the timer repeat value. 879 --- @param timer uv.uv_timer_t 880 --- @return integer repeat_ 881 function uv.timer_get_repeat(timer) end 882 883 --- Get the timer repeat value. 884 --- @return integer repeat_ 885 function uv_timer_t:get_repeat() end 886 887 --- Get the timer due value or 0 if it has expired. The time is relative to `uv.now()`. 888 --- @param timer uv.uv_timer_t 889 --- @return integer due_in 890 function uv.timer_get_due_in(timer) end 891 892 --- Get the timer due value or 0 if it has expired. The time is relative to `uv.now()`. 893 --- @return integer due_in 894 function uv_timer_t:get_due_in() end 895 896 897 --- # `uv_prepare_t` - Prepare handle 898 --- 899 --- > [`uv_handle_t`][] functions also apply. 900 --- 901 --- Prepare handles will run the given callback once per loop iteration, right 902 --- before polling for I/O. 903 --- 904 --- ```lua 905 --- local prepare = uv.new_prepare() 906 --- prepare:start(function() 907 --- print("Before I/O polling") 908 --- end) 909 --- ``` 910 --- @class uv.uv_prepare_t : uv.uv_handle_t 911 local uv_prepare_t = {} 912 913 --- Creates and initializes a new `uv_prepare_t`. Returns the Lua userdata wrapping 914 --- it. 915 --- @return uv.uv_prepare_t 916 function uv.new_prepare() end 917 918 --- Start the handle with the given callback. 919 --- @param prepare uv.uv_prepare_t 920 --- @param callback fun() 921 --- @return 0? success 922 --- @return string? err 923 --- @return uv.error_name? err_name 924 function uv.prepare_start(prepare, callback) end 925 926 --- Start the handle with the given callback. 927 --- @param callback fun() 928 --- @return 0? success 929 --- @return string? err 930 --- @return uv.error_name? err_name 931 function uv_prepare_t:start(callback) end 932 933 --- Stop the handle, the callback will no longer be called. 934 --- @param prepare uv.uv_prepare_t 935 --- @return 0? success 936 --- @return string? err 937 --- @return uv.error_name? err_name 938 function uv.prepare_stop(prepare) end 939 940 --- Stop the handle, the callback will no longer be called. 941 --- @return 0? success 942 --- @return string? err 943 --- @return uv.error_name? err_name 944 function uv_prepare_t:stop() end 945 946 947 --- # `uv_check_t` - Check handle 948 --- 949 --- > [`uv_handle_t`][] functions also apply. 950 --- 951 --- Check handles will run the given callback once per loop iteration, right after 952 --- polling for I/O. 953 --- 954 --- ```lua 955 --- local check = uv.new_check() 956 --- check:start(function() 957 --- print("After I/O polling") 958 --- end) 959 --- ``` 960 --- @class uv.uv_check_t : uv.uv_handle_t 961 local uv_check_t = {} 962 963 --- Creates and initializes a new `uv_check_t`. Returns the Lua userdata wrapping 964 --- it. 965 --- @return uv.uv_check_t 966 function uv.new_check() end 967 968 --- Start the handle with the given callback. 969 --- @param check uv.uv_check_t 970 --- @param callback fun() 971 --- @return 0? success 972 --- @return string? err 973 --- @return uv.error_name? err_name 974 function uv.check_start(check, callback) end 975 976 --- Start the handle with the given callback. 977 --- @param callback fun() 978 --- @return 0? success 979 --- @return string? err 980 --- @return uv.error_name? err_name 981 function uv_check_t:start(callback) end 982 983 --- Stop the handle, the callback will no longer be called. 984 --- @param check uv.uv_check_t 985 --- @return 0? success 986 --- @return string? err 987 --- @return uv.error_name? err_name 988 function uv.check_stop(check) end 989 990 --- Stop the handle, the callback will no longer be called. 991 --- @return 0? success 992 --- @return string? err 993 --- @return uv.error_name? err_name 994 function uv_check_t:stop() end 995 996 997 --- # `uv_idle_t` - Idle handle 998 --- 999 --- > [`uv_handle_t`][] functions also apply. 1000 --- 1001 --- Idle handles will run the given callback once per loop iteration, right before 1002 --- the [`uv_prepare_t`][] handles. 1003 --- 1004 --- **Note**: The notable difference with prepare handles is that when there are 1005 --- active idle handles, the loop will perform a zero timeout poll instead of 1006 --- blocking for I/O. 1007 --- 1008 --- **Warning**: Despite the name, idle handles will get their callbacks called on 1009 --- every loop iteration, not when the loop is actually "idle". 1010 --- 1011 --- ```lua 1012 --- local idle = uv.new_idle() 1013 --- idle:start(function() 1014 --- print("Before I/O polling, no blocking") 1015 --- end) 1016 --- ``` 1017 --- @class uv.uv_idle_t : uv.uv_handle_t 1018 local uv_idle_t = {} 1019 1020 --- Creates and initializes a new `uv_idle_t`. Returns the Lua userdata wrapping 1021 --- it. 1022 --- @return uv.uv_idle_t 1023 function uv.new_idle() end 1024 1025 --- Start the handle with the given callback. 1026 --- @param idle uv.uv_idle_t 1027 --- @param callback fun() 1028 --- @return 0? success 1029 --- @return string? err 1030 --- @return uv.error_name? err_name 1031 function uv.idle_start(idle, callback) end 1032 1033 --- Start the handle with the given callback. 1034 --- @param callback fun() 1035 --- @return 0? success 1036 --- @return string? err 1037 --- @return uv.error_name? err_name 1038 function uv_idle_t:start(callback) end 1039 1040 --- Stop the handle, the callback will no longer be called. 1041 --- @param idle uv.uv_idle_t 1042 --- @return 0? success 1043 --- @return string? err 1044 --- @return uv.error_name? err_name 1045 function uv.idle_stop(idle) end 1046 1047 --- Stop the handle, the callback will no longer be called. 1048 --- @return 0? success 1049 --- @return string? err 1050 --- @return uv.error_name? err_name 1051 function uv_idle_t:stop() end 1052 1053 1054 --- # `uv_async_t` - Async handle 1055 --- 1056 --- > [`uv_handle_t`][] functions also apply. 1057 --- 1058 --- Async handles allow the user to "wakeup" the event loop and get a callback 1059 --- called from another thread. 1060 --- 1061 --- ```lua 1062 --- local async 1063 --- async = uv.new_async(function() 1064 --- print("async operation ran") 1065 --- async:close() 1066 --- end) 1067 --- 1068 --- async:send() 1069 --- ``` 1070 --- @class uv.uv_async_t : uv.uv_handle_t 1071 local uv_async_t = {} 1072 1073 --- Creates and initializes a new `uv_async_t`. Returns the Lua userdata wrapping 1074 --- it. 1075 --- **Note**: 1076 --- Unlike other handle initialization functions, this immediately starts 1077 --- the handle. 1078 --- @param callback fun(...: uv.threadargs) 1079 --- @return uv.uv_async_t? async 1080 --- @return string? err 1081 --- @return uv.error_name? err_name 1082 function uv.new_async(callback) end 1083 1084 --- Wakeup the event loop and call the async handle's callback. 1085 --- **Note**: 1086 --- It's safe to call this function from any thread. The callback will be 1087 --- called on the loop thread. 1088 --- **Warning**: 1089 --- libuv will coalesce calls to `uv.async_send(async)`, that is, not 1090 --- every call to it will yield an execution of the callback. For example: if 1091 --- `uv.async_send()` is called 5 times in a row before the callback is called, the 1092 --- callback will only be called once. If `uv.async_send()` is called again after 1093 --- the callback was called, it will be called again. 1094 --- @param async uv.uv_async_t 1095 --- @param ... uv.threadargs 1096 --- @return 0? success 1097 --- @return string? err 1098 --- @return uv.error_name? err_name 1099 function uv.async_send(async, ...) end 1100 1101 --- Wakeup the event loop and call the async handle's callback. 1102 --- **Note**: 1103 --- It's safe to call this function from any thread. The callback will be 1104 --- called on the loop thread. 1105 --- **Warning**: 1106 --- libuv will coalesce calls to `uv.async_send(async)`, that is, not 1107 --- every call to it will yield an execution of the callback. For example: if 1108 --- `uv.async_send()` is called 5 times in a row before the callback is called, the 1109 --- callback will only be called once. If `uv.async_send()` is called again after 1110 --- the callback was called, it will be called again. 1111 --- @param ... uv.threadargs 1112 --- @return 0? success 1113 --- @return string? err 1114 --- @return uv.error_name? err_name 1115 function uv_async_t:send(...) end 1116 1117 1118 --- # `uv_poll_t` - Poll handle 1119 --- 1120 --- > [`uv_handle_t`][] functions also apply. 1121 --- 1122 --- Poll handles are used to watch file descriptors for readability and writability, 1123 --- similar to the purpose of [poll(2)](http://linux.die.net/man/2/poll). 1124 --- 1125 --- The purpose of poll handles is to enable integrating external libraries that 1126 --- rely on the event loop to signal it about the socket status changes, like c-ares 1127 --- or libssh2. Using `uv_poll_t` for any other purpose is not recommended; 1128 --- `uv_tcp_t`, `uv_udp_t`, etc. provide an implementation that is faster and more 1129 --- scalable than what can be achieved with `uv_poll_t`, especially on Windows. 1130 --- 1131 --- It is possible that poll handles occasionally signal that a file descriptor is 1132 --- readable or writable even when it isn't. The user should therefore always be 1133 --- prepared to handle EAGAIN or equivalent when it attempts to read from or write 1134 --- to the fd. 1135 --- 1136 --- It is not okay to have multiple active poll handles for the same socket, this 1137 --- can cause libuv to busyloop or otherwise malfunction. 1138 --- 1139 --- The user should not close a file descriptor while it is being polled by an 1140 --- active poll handle. This can cause the handle to report an error, but it might 1141 --- also start polling another socket. However the fd can be safely closed 1142 --- immediately after a call to `uv.poll_stop()` or `uv.close()`. 1143 --- 1144 --- **Note**: On windows only sockets can be polled with poll handles. On Unix any 1145 --- file descriptor that would be accepted by poll(2) can be used. 1146 --- @class uv.uv_poll_t : uv.uv_handle_t 1147 local uv_poll_t = {} 1148 1149 --- Initialize the handle using a file descriptor. 1150 --- 1151 --- The file descriptor is set to non-blocking mode. 1152 --- @param fd integer 1153 --- @return uv.uv_poll_t? poll 1154 --- @return string? err 1155 --- @return uv.error_name? err_name 1156 function uv.new_poll(fd) end 1157 1158 --- Initialize the handle using a socket descriptor. On Unix this is identical to 1159 --- `uv.new_poll()`. On windows it takes a SOCKET handle. 1160 --- 1161 --- The socket is set to non-blocking mode. 1162 --- @param fd integer 1163 --- @return uv.uv_poll_t? poll 1164 --- @return string? err 1165 --- @return uv.error_name? err_name 1166 function uv.new_socket_poll(fd) end 1167 1168 --- Starts polling the file descriptor. `events` are: `"r"`, `"w"`, `"rw"`, `"d"`, 1169 --- `"rd"`, `"wd"`, `"rwd"`, `"p"`, `"rp"`, `"wp"`, `"rwp"`, `"dp"`, `"rdp"`, 1170 --- `"wdp"`, or `"rwdp"` where `r` is `READABLE`, `w` is `WRITABLE`, `d` is 1171 --- `DISCONNECT`, and `p` is `PRIORITIZED`. As soon as an event is detected 1172 --- the callback will be called with status set to 0, and the detected events set on 1173 --- the events field. 1174 --- 1175 --- The user should not close the socket while the handle is active. If the user 1176 --- does that anyway, the callback may be called reporting an error status, but this 1177 --- is not guaranteed. 1178 --- **Note**: 1179 --- Calling `uv.poll_start()` on a handle that is already active is fine. 1180 --- Doing so will update the events mask that is being watched for. 1181 --- @param poll uv.uv_poll_t 1182 --- @param events string? 1183 --- @param callback fun(err: string?, events: string?) 1184 --- @return 0? success 1185 --- @return string? err 1186 --- @return uv.error_name? err_name 1187 function uv.poll_start(poll, events, callback) end 1188 1189 --- Starts polling the file descriptor. `events` are: `"r"`, `"w"`, `"rw"`, `"d"`, 1190 --- `"rd"`, `"wd"`, `"rwd"`, `"p"`, `"rp"`, `"wp"`, `"rwp"`, `"dp"`, `"rdp"`, 1191 --- `"wdp"`, or `"rwdp"` where `r` is `READABLE`, `w` is `WRITABLE`, `d` is 1192 --- `DISCONNECT`, and `p` is `PRIORITIZED`. As soon as an event is detected 1193 --- the callback will be called with status set to 0, and the detected events set on 1194 --- the events field. 1195 --- 1196 --- The user should not close the socket while the handle is active. If the user 1197 --- does that anyway, the callback may be called reporting an error status, but this 1198 --- is not guaranteed. 1199 --- **Note**: 1200 --- Calling `uv.poll_start()` on a handle that is already active is fine. 1201 --- Doing so will update the events mask that is being watched for. 1202 --- @param events string? 1203 --- @param callback fun(err: string?, events: string?) 1204 --- @return 0? success 1205 --- @return string? err 1206 --- @return uv.error_name? err_name 1207 function uv_poll_t:start(events, callback) end 1208 1209 --- Stop polling the file descriptor, the callback will no longer be called. 1210 --- @param poll uv.uv_poll_t 1211 --- @return 0? success 1212 --- @return string? err 1213 --- @return uv.error_name? err_name 1214 function uv.poll_stop(poll) end 1215 1216 --- Stop polling the file descriptor, the callback will no longer be called. 1217 --- @return 0? success 1218 --- @return string? err 1219 --- @return uv.error_name? err_name 1220 function uv_poll_t:stop() end 1221 1222 1223 --- # `uv_signal_t` - Signal handle 1224 --- 1225 --- > [`uv_handle_t`][] functions also apply. 1226 --- 1227 --- Signal handles implement Unix style signal handling on a per-event loop bases. 1228 --- 1229 --- **Windows Notes:** 1230 --- 1231 --- Reception of some signals is emulated on Windows: 1232 --- - SIGINT is normally delivered when the user presses CTRL+C. However, like on 1233 --- Unix, it is not generated when terminal raw mode is enabled. 1234 --- - SIGBREAK is delivered when the user pressed CTRL + BREAK. 1235 --- - SIGHUP is generated when the user closes the console window. On SIGHUP the 1236 --- program is given approximately 10 seconds to perform cleanup. After that 1237 --- Windows will unconditionally terminate it. 1238 --- - SIGWINCH is raised whenever libuv detects that the console has been resized. 1239 --- SIGWINCH is emulated by libuv when the program uses a uv_tty_t handle to write 1240 --- to the console. SIGWINCH may not always be delivered in a timely manner; libuv 1241 --- will only detect size changes when the cursor is being moved. When a readable 1242 --- [`uv_tty_t`][] handle is used in raw mode, resizing the console buffer will 1243 --- also trigger a SIGWINCH signal. 1244 --- - Watchers for other signals can be successfully created, but these signals 1245 --- are never received. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV, 1246 --- SIGTERM and SIGKILL. 1247 --- - Calls to raise() or abort() to programmatically raise a signal are not 1248 --- detected by libuv; these will not trigger a signal watcher. 1249 --- 1250 --- **Unix Notes:** 1251 --- 1252 --- - SIGKILL and SIGSTOP are impossible to catch. 1253 --- - Handling SIGBUS, SIGFPE, SIGILL or SIGSEGV via libuv results into undefined 1254 --- behavior. 1255 --- - SIGABRT will not be caught by libuv if generated by abort(), e.g. through 1256 --- assert(). 1257 --- - On Linux SIGRT0 and SIGRT1 (signals 32 and 33) are used by the NPTL pthreads 1258 --- library to manage threads. Installing watchers for those signals will lead to 1259 --- unpredictable behavior and is strongly discouraged. Future versions of libuv 1260 --- may simply reject them. 1261 --- 1262 --- ```lua 1263 --- -- Create a new signal handler 1264 --- local signal = uv.new_signal() 1265 --- -- Define a handler function 1266 --- uv.signal_start(signal, "sigint", function(signame) 1267 --- print("got " .. signame .. ", shutting down") 1268 --- os.exit(1) 1269 --- end) 1270 --- ``` 1271 --- @class uv.uv_signal_t : uv.uv_handle_t 1272 local uv_signal_t = {} 1273 1274 --- Creates and initializes a new `uv_signal_t`. Returns the Lua userdata wrapping 1275 --- it. 1276 --- @return uv.uv_signal_t? signal 1277 --- @return string? err 1278 --- @return uv.error_name? err_name 1279 function uv.new_signal() end 1280 1281 --- Start the handle with the given callback, watching for the given signal. 1282 --- 1283 --- See [Constants][] for supported `signame` input and output values. 1284 --- @param signal uv.uv_signal_t 1285 --- @param signame string|integer 1286 --- @param callback fun(signame: string) 1287 --- @return 0? success 1288 --- @return string? err 1289 --- @return uv.error_name? err_name 1290 function uv.signal_start(signal, signame, callback) end 1291 1292 --- Start the handle with the given callback, watching for the given signal. 1293 --- 1294 --- See [Constants][] for supported `signame` input and output values. 1295 --- @param signame string|integer 1296 --- @param callback fun(signame: string) 1297 --- @return 0? success 1298 --- @return string? err 1299 --- @return uv.error_name? err_name 1300 function uv_signal_t:start(signame, callback) end 1301 1302 --- Same functionality as `uv.signal_start()` but the signal handler is reset the moment the signal is received. 1303 --- 1304 --- See [Constants][] for supported `signame` input and output values. 1305 --- @param signal uv.uv_signal_t 1306 --- @param signame string|integer 1307 --- @param callback fun(signame: string) 1308 --- @return 0? success 1309 --- @return string? err 1310 --- @return uv.error_name? err_name 1311 function uv.signal_start_oneshot(signal, signame, callback) end 1312 1313 --- Same functionality as `uv.signal_start()` but the signal handler is reset the moment the signal is received. 1314 --- 1315 --- See [Constants][] for supported `signame` input and output values. 1316 --- @param signame string|integer 1317 --- @param callback fun(signame: string) 1318 --- @return 0? success 1319 --- @return string? err 1320 --- @return uv.error_name? err_name 1321 function uv_signal_t:start_oneshot(signame, callback) end 1322 1323 --- Stop the handle, the callback will no longer be called. 1324 --- @param signal uv.uv_signal_t 1325 --- @return 0? success 1326 --- @return string? err 1327 --- @return uv.error_name? err_name 1328 function uv.signal_stop(signal) end 1329 1330 --- Stop the handle, the callback will no longer be called. 1331 --- @return 0? success 1332 --- @return string? err 1333 --- @return uv.error_name? err_name 1334 function uv_signal_t:stop() end 1335 1336 1337 --- # `uv_process_t` - Process handle 1338 --- 1339 --- > [`uv_handle_t`][] functions also apply. 1340 --- 1341 --- Process handles will spawn a new process and allow the user to control it and 1342 --- establish communication channels with it using streams. 1343 --- @class uv.uv_process_t : uv.uv_handle_t 1344 local uv_process_t = {} 1345 1346 --- Disables inheritance for file descriptors / handles that this process inherited 1347 --- from its parent. The effect is that child processes spawned by this process 1348 --- don't accidentally inherit these handles. 1349 --- 1350 --- It is recommended to call this function as early in your program as possible, 1351 --- before the inherited file descriptors can be closed or duplicated. 1352 --- **Note**: 1353 --- This function works on a best-effort basis: there is no guarantee that 1354 --- libuv can discover all file descriptors that were inherited. In general it does 1355 --- a better job on Windows than it does on Unix. 1356 function uv.disable_stdio_inheritance() end 1357 1358 --- @class uv.spawn.options 1359 --- 1360 --- Command line arguments as a list of strings. The first 1361 --- string should *not* be the path to the program, since that is already 1362 --- provided via `path`. On Windows, this uses CreateProcess which concatenates 1363 --- the arguments into a string. This can cause some strange errors 1364 --- (see `options.verbatim` below for Windows). 1365 --- @field args string[]? 1366 --- 1367 --- Set the file descriptors that will be made available to 1368 --- the child process. The convention is that the first entries are stdin, stdout, 1369 --- and stderr. 1370 --- 1371 --- The entries can take many shapes. 1372 --- - If `integer`, then the child process inherits that same zero-indexed 1373 --- fd from the parent process. 1374 --- - If `uv_stream_t` handles are passed in, those are used as a read-write pipe 1375 --- or inherited stream depending if the stream has a valid fd. 1376 --- - If `nil`, means to ignore that fd in the child process. 1377 --- 1378 --- **Note**: On Windows, file descriptors after the third are 1379 --- available to the child process only if the child processes uses the MSVCRT 1380 --- runtime. 1381 --- @field stdio table<integer, integer|uv.uv_stream_t?>? 1382 --- 1383 --- Set environment variables for the new process. 1384 --- Each entry should be a string in the form of `NAME=VALUE`. 1385 --- @field env string[]? 1386 --- 1387 --- Set the current working directory for the sub-process. 1388 --- @field cwd string? 1389 --- 1390 --- Set the child process' user id. 1391 --- @field uid string? 1392 --- 1393 --- Set the child process' group id. 1394 --- @field gid string? 1395 --- 1396 --- If true, do not wrap any arguments in quotes, or 1397 --- perform any other escaping, when converting the argument list into a command 1398 --- line string. This option is only meaningful on Windows systems. On Unix it is 1399 --- silently ignored. 1400 --- @field verbatim boolean? 1401 --- 1402 --- If true, spawn the child process in a detached state - 1403 --- this will make it a process group leader, and will effectively enable the 1404 --- child to keep running after the parent exits. Note that the child process 1405 --- will still keep the parent's event loop alive unless the parent process calls 1406 --- `uv.unref()` on the child's process handle. 1407 --- @field detached boolean? 1408 --- 1409 --- If true, hide the subprocess console window that would 1410 --- normally be created. This option is only meaningful on Windows systems. On 1411 --- Unix it is silently ignored. 1412 --- @field hide boolean? 1413 1414 --- Initializes the process handle and starts the process. If the process is 1415 --- successfully spawned, this function will return the handle and pid of the child 1416 --- process. 1417 --- 1418 --- Possible reasons for failing to spawn would include (but not be limited to) the 1419 --- file to execute not existing, not having permissions to use the setuid or setgid 1420 --- specified, or not having enough memory to allocate for the new process. 1421 --- 1422 --- ```lua 1423 --- local stdin = uv.new_pipe() 1424 --- local stdout = uv.new_pipe() 1425 --- local stderr = uv.new_pipe() 1426 --- 1427 --- print("stdin", stdin) 1428 --- print("stdout", stdout) 1429 --- print("stderr", stderr) 1430 --- 1431 --- local handle, pid = uv.spawn("cat", { 1432 --- stdio = {stdin, stdout, stderr} 1433 --- }, function(code, signal) -- on exit 1434 --- print("exit code", code) 1435 --- print("exit signal", signal) 1436 --- end) 1437 --- 1438 --- print("process opened", handle, pid) 1439 --- 1440 --- uv.read_start(stdout, function(err, data) 1441 --- assert(not err, err) 1442 --- if data then 1443 --- print("stdout chunk", stdout, data) 1444 --- else 1445 --- print("stdout end", stdout) 1446 --- end 1447 --- end) 1448 --- 1449 --- uv.read_start(stderr, function(err, data) 1450 --- assert(not err, err) 1451 --- if data then 1452 --- print("stderr chunk", stderr, data) 1453 --- else 1454 --- print("stderr end", stderr) 1455 --- end 1456 --- end) 1457 --- 1458 --- uv.write(stdin, "Hello World") 1459 --- 1460 --- uv.shutdown(stdin, function() 1461 --- print("stdin shutdown", stdin) 1462 --- uv.close(handle, function() 1463 --- print("process closed", handle, pid) 1464 --- end) 1465 --- end) 1466 --- ``` 1467 --- 1468 --- When the child process exits, `on_exit` is called with an exit code and signal. 1469 --- @param path string 1470 --- @param options uv.spawn.options 1471 --- @param on_exit fun(code: integer, signal: integer) 1472 --- @return uv.uv_process_t? handle 1473 --- @return integer|string pid_or_err 1474 --- @return uv.error_name? err_name 1475 function uv.spawn(path, options, on_exit) end 1476 1477 --- Sends the specified signal to the given process handle. Check the documentation 1478 --- on `uv_signal_t` for signal support, specially on Windows. 1479 --- 1480 --- See [Constants][] for supported `signame` input values. 1481 --- @param process uv.uv_process_t 1482 --- @param signame string|integer? 1483 --- @return 0? success 1484 --- @return string? err 1485 --- @return uv.error_name? err_name 1486 function uv.process_kill(process, signame) end 1487 1488 --- Sends the specified signal to the given process handle. Check the documentation 1489 --- on `uv_signal_t` for signal support, specially on Windows. 1490 --- 1491 --- See [Constants][] for supported `signame` input values. 1492 --- @param signame string|integer? 1493 --- @return 0? success 1494 --- @return string? err 1495 --- @return uv.error_name? err_name 1496 function uv_process_t:kill(signame) end 1497 1498 --- Sends the specified signal to the given PID. Check the documentation on 1499 --- `uv_signal_t` for signal support, specially on Windows. 1500 --- 1501 --- See [Constants][] for supported `signame` input values. 1502 --- @param pid integer 1503 --- @param signame string|integer? 1504 --- @return 0? success 1505 --- @return string? err 1506 --- @return uv.error_name? err_name 1507 function uv.kill(pid, signame) end 1508 1509 --- Returns the handle's pid. 1510 --- @param process uv.uv_process_t 1511 --- @return integer 1512 function uv.process_get_pid(process) end 1513 1514 --- Returns the handle's pid. 1515 --- @return integer 1516 function uv_process_t:get_pid() end 1517 1518 1519 --- # `uv_stream_t` - Stream handle 1520 --- 1521 --- > [`uv_handle_t`][] functions also apply. 1522 --- 1523 --- Stream handles provide an abstraction of a duplex communication channel. 1524 --- [`uv_stream_t`][] is an abstract type, libuv provides 3 stream implementations 1525 --- in the form of [`uv_tcp_t`][], [`uv_pipe_t`][] and [`uv_tty_t`][]. 1526 --- @class uv.uv_stream_t : uv.uv_handle_t 1527 local uv_stream_t = {} 1528 1529 --- Shutdown the outgoing (write) side of a duplex stream. It waits for pending 1530 --- write requests to complete. The callback is called after shutdown is complete. 1531 --- @param stream uv.uv_stream_t 1532 --- @param callback fun(err: string?)? 1533 --- @return uv.uv_shutdown_t? shutdown 1534 --- @return string? err 1535 --- @return uv.error_name? err_name 1536 function uv.shutdown(stream, callback) end 1537 1538 --- Shutdown the outgoing (write) side of a duplex stream. It waits for pending 1539 --- write requests to complete. The callback is called after shutdown is complete. 1540 --- @param callback fun(err: string?)? 1541 --- @return uv.uv_shutdown_t? shutdown 1542 --- @return string? err 1543 --- @return uv.error_name? err_name 1544 function uv_stream_t:shutdown(callback) end 1545 1546 --- Start listening for incoming connections. `backlog` indicates the number of 1547 --- connections the kernel might queue, same as `listen(2)`. When a new incoming 1548 --- connection is received the callback is called. 1549 --- @param stream uv.uv_stream_t 1550 --- @param backlog integer 1551 --- @param callback fun(err: string?) 1552 --- @return 0? success 1553 --- @return string? err 1554 --- @return uv.error_name? err_name 1555 function uv.listen(stream, backlog, callback) end 1556 1557 --- Start listening for incoming connections. `backlog` indicates the number of 1558 --- connections the kernel might queue, same as `listen(2)`. When a new incoming 1559 --- connection is received the callback is called. 1560 --- @param backlog integer 1561 --- @param callback fun(err: string?) 1562 --- @return 0? success 1563 --- @return string? err 1564 --- @return uv.error_name? err_name 1565 function uv_stream_t:listen(backlog, callback) end 1566 1567 --- This call is used in conjunction with `uv.listen()` to accept incoming 1568 --- connections. Call this function after receiving a callback to accept the 1569 --- connection. 1570 --- 1571 --- When the connection callback is called it is guaranteed that this function 1572 --- will complete successfully the first time. If you attempt to use it more than 1573 --- once, it may fail. It is suggested to only call this function once per 1574 --- connection call. 1575 --- Example 1576 --- ```lua 1577 --- server:listen(128, function (err) 1578 --- local client = uv.new_tcp() 1579 --- server:accept(client) 1580 --- end) 1581 --- ``` 1582 --- @param stream uv.uv_stream_t 1583 --- @param client_stream uv.uv_stream_t 1584 --- @return 0? success 1585 --- @return string? err 1586 --- @return uv.error_name? err_name 1587 function uv.accept(stream, client_stream) end 1588 1589 --- This call is used in conjunction with `uv.listen()` to accept incoming 1590 --- connections. Call this function after receiving a callback to accept the 1591 --- connection. 1592 --- 1593 --- When the connection callback is called it is guaranteed that this function 1594 --- will complete successfully the first time. If you attempt to use it more than 1595 --- once, it may fail. It is suggested to only call this function once per 1596 --- connection call. 1597 --- Example 1598 --- ```lua 1599 --- server:listen(128, function (err) 1600 --- local client = uv.new_tcp() 1601 --- server:accept(client) 1602 --- end) 1603 --- ``` 1604 --- @param client_stream uv.uv_stream_t 1605 --- @return 0? success 1606 --- @return string? err 1607 --- @return uv.error_name? err_name 1608 function uv_stream_t:accept(client_stream) end 1609 1610 --- Read data from an incoming stream. The callback will be made several times until 1611 --- there is no more data to read or `uv.read_stop()` is called. When we've reached 1612 --- EOF, `data` will be `nil`. 1613 --- Example 1614 --- ```lua 1615 --- stream:read_start(function (err, chunk) 1616 --- if err then 1617 --- -- handle read error 1618 --- elseif chunk then 1619 --- -- handle data 1620 --- else 1621 --- -- handle disconnect 1622 --- end 1623 --- end) 1624 --- ``` 1625 --- @param stream uv.uv_stream_t 1626 --- @param callback fun(err: string?, data: string?) 1627 --- @return 0? success 1628 --- @return string? err 1629 --- @return uv.error_name? err_name 1630 function uv.read_start(stream, callback) end 1631 1632 --- Read data from an incoming stream. The callback will be made several times until 1633 --- there is no more data to read or `uv.read_stop()` is called. When we've reached 1634 --- EOF, `data` will be `nil`. 1635 --- Example 1636 --- ```lua 1637 --- stream:read_start(function (err, chunk) 1638 --- if err then 1639 --- -- handle read error 1640 --- elseif chunk then 1641 --- -- handle data 1642 --- else 1643 --- -- handle disconnect 1644 --- end 1645 --- end) 1646 --- ``` 1647 --- @param callback fun(err: string?, data: string?) 1648 --- @return 0? success 1649 --- @return string? err 1650 --- @return uv.error_name? err_name 1651 function uv_stream_t:read_start(callback) end 1652 1653 --- Stop reading data from the stream. The read callback will no longer be called. 1654 --- 1655 --- This function is idempotent and may be safely called on a stopped stream. 1656 --- @param stream uv.uv_stream_t 1657 --- @return 0? success 1658 --- @return string? err 1659 --- @return uv.error_name? err_name 1660 function uv.read_stop(stream) end 1661 1662 --- Stop reading data from the stream. The read callback will no longer be called. 1663 --- 1664 --- This function is idempotent and may be safely called on a stopped stream. 1665 --- @return 0? success 1666 --- @return string? err 1667 --- @return uv.error_name? err_name 1668 function uv_stream_t:read_stop() end 1669 1670 --- Write data to stream. 1671 --- 1672 --- `data` can either be a Lua string or a table of strings. If a table is passed 1673 --- in, the C backend will use writev to send all strings in a single system call. 1674 --- 1675 --- The optional `callback` is for knowing when the write is complete. 1676 --- @param stream uv.uv_stream_t 1677 --- @param data uv.buffer 1678 --- @param callback fun(err: string?)? 1679 --- @return uv.uv_write_t? write 1680 --- @return string? err 1681 --- @return uv.error_name? err_name 1682 function uv.write(stream, data, callback) end 1683 1684 --- Write data to stream. 1685 --- 1686 --- `data` can either be a Lua string or a table of strings. If a table is passed 1687 --- in, the C backend will use writev to send all strings in a single system call. 1688 --- 1689 --- The optional `callback` is for knowing when the write is complete. 1690 --- @param data uv.buffer 1691 --- @param callback fun(err: string?)? 1692 --- @return uv.uv_write_t? write 1693 --- @return string? err 1694 --- @return uv.error_name? err_name 1695 function uv_stream_t:write(data, callback) end 1696 1697 --- Extended write function for sending handles over a pipe. The pipe must be 1698 --- initialized with `ipc` option `true`. 1699 --- **Note**: 1700 --- `send_handle` must be a TCP socket or pipe, which is a server or a 1701 --- connection (listening or connected state). Bound sockets or pipes will be 1702 --- assumed to be servers. 1703 --- @param stream uv.uv_stream_t 1704 --- @param data uv.buffer 1705 --- @param send_handle uv.uv_stream_t 1706 --- @param callback fun(err: string?)? 1707 --- @return uv.uv_write_t? write 1708 --- @return string? err 1709 --- @return uv.error_name? err_name 1710 function uv.write2(stream, data, send_handle, callback) end 1711 1712 --- Extended write function for sending handles over a pipe. The pipe must be 1713 --- initialized with `ipc` option `true`. 1714 --- **Note**: 1715 --- `send_handle` must be a TCP socket or pipe, which is a server or a 1716 --- connection (listening or connected state). Bound sockets or pipes will be 1717 --- assumed to be servers. 1718 --- @param data uv.buffer 1719 --- @param send_handle uv.uv_stream_t 1720 --- @param callback fun(err: string?)? 1721 --- @return uv.uv_write_t? write 1722 --- @return string? err 1723 --- @return uv.error_name? err_name 1724 function uv_stream_t:write2(data, send_handle, callback) end 1725 1726 --- Same as `uv.write()`, but won't queue a write request if it can't be completed 1727 --- immediately. 1728 --- 1729 --- Will return number of bytes written (can be less than the supplied buffer size). 1730 --- @param stream uv.uv_stream_t 1731 --- @param data uv.buffer 1732 --- @return integer? bytes_written 1733 --- @return string? err 1734 --- @return uv.error_name? err_name 1735 function uv.try_write(stream, data) end 1736 1737 --- Same as `uv.write()`, but won't queue a write request if it can't be completed 1738 --- immediately. 1739 --- 1740 --- Will return number of bytes written (can be less than the supplied buffer size). 1741 --- @param data uv.buffer 1742 --- @return integer? bytes_written 1743 --- @return string? err 1744 --- @return uv.error_name? err_name 1745 function uv_stream_t:try_write(data) end 1746 1747 --- Like `uv.write2()`, but with the properties of `uv.try_write()`. Not supported on Windows, where it returns `UV_EAGAIN`. 1748 --- 1749 --- Will return number of bytes written (can be less than the supplied buffer size). 1750 --- @param stream uv.uv_stream_t 1751 --- @param data uv.buffer 1752 --- @param send_handle uv.uv_stream_t 1753 --- @return integer? bytes_written 1754 --- @return string? err 1755 --- @return uv.error_name? err_name 1756 function uv.try_write2(stream, data, send_handle) end 1757 1758 --- Like `uv.write2()`, but with the properties of `uv.try_write()`. Not supported on Windows, where it returns `UV_EAGAIN`. 1759 --- 1760 --- Will return number of bytes written (can be less than the supplied buffer size). 1761 --- @param data uv.buffer 1762 --- @param send_handle uv.uv_stream_t 1763 --- @return integer? bytes_written 1764 --- @return string? err 1765 --- @return uv.error_name? err_name 1766 function uv_stream_t:try_write2(data, send_handle) end 1767 1768 --- Returns `true` if the stream is readable, `false` otherwise. 1769 --- @param stream uv.uv_stream_t 1770 --- @return boolean 1771 function uv.is_readable(stream) end 1772 1773 --- Returns `true` if the stream is readable, `false` otherwise. 1774 --- @return boolean 1775 function uv_stream_t:is_readable() end 1776 1777 --- Returns `true` if the stream is writable, `false` otherwise. 1778 --- @param stream uv.uv_stream_t 1779 --- @return boolean 1780 function uv.is_writable(stream) end 1781 1782 --- Returns `true` if the stream is writable, `false` otherwise. 1783 --- @return boolean 1784 function uv_stream_t:is_writable() end 1785 1786 --- Enable or disable blocking mode for a stream. 1787 --- 1788 --- When blocking mode is enabled all writes complete synchronously. The interface 1789 --- remains unchanged otherwise, e.g. completion or failure of the operation will 1790 --- still be reported through a callback which is made asynchronously. 1791 --- **Warning**: 1792 --- Relying too much on this API is not recommended. It is likely to 1793 --- change significantly in the future. Currently this only works on Windows and 1794 --- only for `uv_pipe_t` handles. Also libuv currently makes no ordering guarantee 1795 --- when the blocking mode is changed after write requests have already been 1796 --- submitted. Therefore it is recommended to set the blocking mode immediately 1797 --- after opening or creating the stream. 1798 --- @param stream uv.uv_stream_t 1799 --- @param blocking boolean 1800 --- @return 0? success 1801 --- @return string? err 1802 --- @return uv.error_name? err_name 1803 function uv.stream_set_blocking(stream, blocking) end 1804 1805 --- Enable or disable blocking mode for a stream. 1806 --- 1807 --- When blocking mode is enabled all writes complete synchronously. The interface 1808 --- remains unchanged otherwise, e.g. completion or failure of the operation will 1809 --- still be reported through a callback which is made asynchronously. 1810 --- **Warning**: 1811 --- Relying too much on this API is not recommended. It is likely to 1812 --- change significantly in the future. Currently this only works on Windows and 1813 --- only for `uv_pipe_t` handles. Also libuv currently makes no ordering guarantee 1814 --- when the blocking mode is changed after write requests have already been 1815 --- submitted. Therefore it is recommended to set the blocking mode immediately 1816 --- after opening or creating the stream. 1817 --- @param blocking boolean 1818 --- @return 0? success 1819 --- @return string? err 1820 --- @return uv.error_name? err_name 1821 function uv_stream_t:set_blocking(blocking) end 1822 1823 --- Returns the stream's write queue size. 1824 --- @param stream uv.uv_stream_t 1825 --- @return integer 1826 function uv.stream_get_write_queue_size(stream) end 1827 1828 --- Returns the stream's write queue size. 1829 --- @return integer 1830 function uv_stream_t:get_write_queue_size() end 1831 1832 1833 --- # `uv_tcp_t` - TCP handle 1834 --- 1835 --- > [`uv_handle_t`][] and [`uv_stream_t`][] functions also apply. 1836 --- 1837 --- TCP handles are used to represent both TCP streams and servers. 1838 --- @class uv.uv_tcp_t : uv.uv_stream_t 1839 local uv_tcp_t = {} 1840 1841 --- Creates and initializes a new `uv_tcp_t`. Returns the Lua userdata wrapping it. 1842 --- 1843 --- If set, `flags` must be a valid address family. See [Constants][] for supported 1844 --- address family input values. 1845 --- @param flags string|integer? 1846 --- @return uv.uv_tcp_t? tcp 1847 --- @return string? err 1848 --- @return uv.error_name? err_name 1849 function uv.new_tcp(flags) end 1850 1851 --- Open an existing file descriptor or SOCKET as a TCP handle. 1852 --- **Note**: 1853 --- The passed file descriptor or SOCKET is not checked for its type, but it's required that it represents a valid stream socket. 1854 --- @param tcp uv.uv_tcp_t 1855 --- @param sock integer 1856 --- @return 0? success 1857 --- @return string? err 1858 --- @return uv.error_name? err_name 1859 function uv.tcp_open(tcp, sock) end 1860 1861 --- Open an existing file descriptor or SOCKET as a TCP handle. 1862 --- **Note**: 1863 --- The passed file descriptor or SOCKET is not checked for its type, but it's required that it represents a valid stream socket. 1864 --- @param sock integer 1865 --- @return 0? success 1866 --- @return string? err 1867 --- @return uv.error_name? err_name 1868 function uv_tcp_t:open(sock) end 1869 1870 --- Enable / disable Nagle's algorithm. 1871 --- @param tcp uv.uv_tcp_t 1872 --- @param enable boolean 1873 --- @return 0? success 1874 --- @return string? err 1875 --- @return uv.error_name? err_name 1876 function uv.tcp_nodelay(tcp, enable) end 1877 1878 --- Enable / disable Nagle's algorithm. 1879 --- @param enable boolean 1880 --- @return 0? success 1881 --- @return string? err 1882 --- @return uv.error_name? err_name 1883 function uv_tcp_t:nodelay(enable) end 1884 1885 --- Enable / disable TCP keep-alive. `delay` is the initial delay in seconds, 1886 --- ignored when enable is `false`. 1887 --- @param tcp uv.uv_tcp_t 1888 --- @param enable boolean 1889 --- @param delay integer? 1890 --- @return 0? success 1891 --- @return string? err 1892 --- @return uv.error_name? err_name 1893 function uv.tcp_keepalive(tcp, enable, delay) end 1894 1895 --- Enable / disable TCP keep-alive. `delay` is the initial delay in seconds, 1896 --- ignored when enable is `false`. 1897 --- @param enable boolean 1898 --- @param delay integer? 1899 --- @return 0? success 1900 --- @return string? err 1901 --- @return uv.error_name? err_name 1902 function uv_tcp_t:keepalive(enable, delay) end 1903 1904 --- Enable / disable simultaneous asynchronous accept requests that are queued by 1905 --- the operating system when listening for new TCP connections. 1906 --- 1907 --- This setting is used to tune a TCP server for the desired performance. Having 1908 --- simultaneous accepts can significantly improve the rate of accepting connections 1909 --- (which is why it is enabled by default) but may lead to uneven load distribution 1910 --- in multi-process setups. 1911 --- @param tcp uv.uv_tcp_t 1912 --- @param enable boolean 1913 --- @return 0? success 1914 --- @return string? err 1915 --- @return uv.error_name? err_name 1916 function uv.tcp_simultaneous_accepts(tcp, enable) end 1917 1918 --- Enable / disable simultaneous asynchronous accept requests that are queued by 1919 --- the operating system when listening for new TCP connections. 1920 --- 1921 --- This setting is used to tune a TCP server for the desired performance. Having 1922 --- simultaneous accepts can significantly improve the rate of accepting connections 1923 --- (which is why it is enabled by default) but may lead to uneven load distribution 1924 --- in multi-process setups. 1925 --- @param enable boolean 1926 --- @return 0? success 1927 --- @return string? err 1928 --- @return uv.error_name? err_name 1929 function uv_tcp_t:simultaneous_accepts(enable) end 1930 1931 --- Bind the handle to an host and port. `host` should be an IP address and 1932 --- not a domain name. Any `flags` are set with a table with field `ipv6only` 1933 --- equal to `true` or `false`. 1934 --- 1935 --- When the port is already taken, you can expect to see an `EADDRINUSE` error 1936 --- from either `uv.tcp_bind()`, `uv.listen()` or `uv.tcp_connect()`. That is, a 1937 --- successful call to this function does not guarantee that the call to `uv.listen()` 1938 --- or `uv.tcp_connect()` will succeed as well. 1939 --- 1940 --- Use a port of `0` to let the OS assign an ephemeral port. You can look it up 1941 --- later using `uv.tcp_getsockname()`. 1942 --- @param tcp uv.uv_tcp_t 1943 --- @param host string 1944 --- @param port integer 1945 --- @param flags { ipv6only: boolean }? 1946 --- @return 0? success 1947 --- @return string? err 1948 --- @return uv.error_name? err_name 1949 function uv.tcp_bind(tcp, host, port, flags) end 1950 1951 --- Bind the handle to an host and port. `host` should be an IP address and 1952 --- not a domain name. Any `flags` are set with a table with field `ipv6only` 1953 --- equal to `true` or `false`. 1954 --- 1955 --- When the port is already taken, you can expect to see an `EADDRINUSE` error 1956 --- from either `uv.tcp_bind()`, `uv.listen()` or `uv.tcp_connect()`. That is, a 1957 --- successful call to this function does not guarantee that the call to `uv.listen()` 1958 --- or `uv.tcp_connect()` will succeed as well. 1959 --- 1960 --- Use a port of `0` to let the OS assign an ephemeral port. You can look it up 1961 --- later using `uv.tcp_getsockname()`. 1962 --- @param host string 1963 --- @param port integer 1964 --- @param flags { ipv6only: boolean }? 1965 --- @return 0? success 1966 --- @return string? err 1967 --- @return uv.error_name? err_name 1968 function uv_tcp_t:bind(host, port, flags) end 1969 1970 --- Get the address of the peer connected to the handle. 1971 --- 1972 --- See [Constants][] for supported address `family` output values. 1973 --- @param tcp uv.uv_tcp_t 1974 --- @return uv.socketinfo? address 1975 --- @return string? err 1976 --- @return uv.error_name? err_name 1977 function uv.tcp_getpeername(tcp) end 1978 1979 --- Get the address of the peer connected to the handle. 1980 --- 1981 --- See [Constants][] for supported address `family` output values. 1982 --- @return uv.socketinfo? address 1983 --- @return string? err 1984 --- @return uv.error_name? err_name 1985 function uv_tcp_t:getpeername() end 1986 1987 --- Get the current address to which the handle is bound. 1988 --- 1989 --- See [Constants][] for supported address `family` output values. 1990 --- @param tcp uv.uv_tcp_t 1991 --- @return uv.socketinfo? address 1992 --- @return string? err 1993 --- @return uv.error_name? err_name 1994 function uv.tcp_getsockname(tcp) end 1995 1996 --- Get the current address to which the handle is bound. 1997 --- 1998 --- See [Constants][] for supported address `family` output values. 1999 --- @return uv.socketinfo? address 2000 --- @return string? err 2001 --- @return uv.error_name? err_name 2002 function uv_tcp_t:getsockname() end 2003 2004 --- Establish an IPv4 or IPv6 TCP connection. 2005 --- Example 2006 --- ```lua 2007 --- local client = uv.new_tcp() 2008 --- client:connect("127.0.0.1", 8080, function (err) 2009 --- -- check error and carry on. 2010 --- end) 2011 --- ``` 2012 --- @param tcp uv.uv_tcp_t 2013 --- @param host string 2014 --- @param port integer 2015 --- @param callback fun(err: string?) 2016 --- @return uv.uv_connect_t? connect 2017 --- @return string? err 2018 --- @return uv.error_name? err_name 2019 function uv.tcp_connect(tcp, host, port, callback) end 2020 2021 --- Establish an IPv4 or IPv6 TCP connection. 2022 --- Example 2023 --- ```lua 2024 --- local client = uv.new_tcp() 2025 --- client:connect("127.0.0.1", 8080, function (err) 2026 --- -- check error and carry on. 2027 --- end) 2028 --- ``` 2029 --- @param host string 2030 --- @param port integer 2031 --- @param callback fun(err: string?) 2032 --- @return uv.uv_connect_t? connect 2033 --- @return string? err 2034 --- @return uv.error_name? err_name 2035 function uv_tcp_t:connect(host, port, callback) end 2036 2037 --- @deprecated Please use `uv.stream_get_write_queue_size()` instead. 2038 --- @param tcp uv.uv_tcp_t 2039 function uv.tcp_write_queue_size(tcp) end 2040 2041 --- @deprecated Please use `uv.stream_get_write_queue_size()` instead. 2042 function uv_tcp_t:write_queue_size() end 2043 2044 --- Resets a TCP connection by sending a RST packet. This is accomplished by setting 2045 --- the SO_LINGER socket option with a linger interval of zero and then calling 2046 --- `uv.close()`. Due to some platform inconsistencies, mixing of `uv.shutdown()` 2047 --- and `uv.tcp_close_reset()` calls is not allowed. 2048 --- @param tcp uv.uv_tcp_t 2049 --- @param callback fun()? 2050 --- @return 0? success 2051 --- @return string? err 2052 --- @return uv.error_name? err_name 2053 function uv.tcp_close_reset(tcp, callback) end 2054 2055 --- Resets a TCP connection by sending a RST packet. This is accomplished by setting 2056 --- the SO_LINGER socket option with a linger interval of zero and then calling 2057 --- `uv.close()`. Due to some platform inconsistencies, mixing of `uv.shutdown()` 2058 --- and `uv.tcp_close_reset()` calls is not allowed. 2059 --- @param callback fun()? 2060 --- @return 0? success 2061 --- @return string? err 2062 --- @return uv.error_name? err_name 2063 function uv_tcp_t:close_reset(callback) end 2064 2065 --- Create a pair of connected sockets with the specified properties. The resulting handles can be passed to `uv.tcp_open`, used with `uv.spawn`, or for any other purpose. 2066 --- 2067 --- See [Constants][] for supported `socktype` input values. 2068 --- 2069 --- When `protocol` is set to 0 or nil, it will be automatically chosen based on the socket's domain and type. When `protocol` is specified as a string, it will be looked up using the `getprotobyname(3)` function (examples: `"ip"`, `"icmp"`, `"tcp"`, `"udp"`, etc). 2070 --- 2071 --- Flags: 2072 --- - `nonblock`: Opens the specified socket handle for `OVERLAPPED` or `FIONBIO`/`O_NONBLOCK` I/O usage. This is recommended for handles that will be used by libuv, and not usually recommended otherwise. 2073 --- 2074 --- Equivalent to `socketpair(2)` with a domain of `AF_UNIX`. 2075 --- Example 2076 --- ```lua 2077 --- -- Simple read/write with tcp 2078 --- local fds = uv.socketpair(nil, nil, {nonblock=true}, {nonblock=true}) 2079 --- 2080 --- local sock1 = uv.new_tcp() 2081 --- sock1:open(fds[1]) 2082 --- 2083 --- local sock2 = uv.new_tcp() 2084 --- sock2:open(fds[2]) 2085 --- 2086 --- sock1:write("hello") 2087 --- sock2:read_start(function(err, chunk) 2088 --- assert(not err, err) 2089 --- print(chunk) 2090 --- end) 2091 --- ``` 2092 --- @param socktype string|integer? 2093 --- @param protocol string|integer? 2094 --- @param flags1 { nonblock: boolean }? 2095 --- @param flags2 { nonblock: boolean }? 2096 --- @return [integer, integer]? fds 2097 --- @return string? err 2098 --- @return uv.error_name? err_name 2099 function uv.socketpair(socktype, protocol, flags1, flags2) end 2100 2101 2102 --- # `uv_pipe_t` - Pipe handle 2103 --- 2104 --- > [`uv_handle_t`][] and [`uv_stream_t`][] functions also apply. 2105 --- 2106 --- Pipe handles provide an abstraction over local domain sockets on Unix and named pipes on Windows. 2107 --- 2108 --- ```lua 2109 --- local pipe = uv.new_pipe(false) 2110 --- 2111 --- pipe:bind('/tmp/sock.test') 2112 --- 2113 --- pipe:listen(128, function() 2114 --- local client = uv.new_pipe(false) 2115 --- pipe:accept(client) 2116 --- client:write("hello!\n") 2117 --- client:close() 2118 --- end) 2119 --- ``` 2120 --- @class uv.uv_pipe_t : uv.uv_stream_t 2121 local uv_pipe_t = {} 2122 2123 --- Creates and initializes a new `uv_pipe_t`. Returns the Lua userdata wrapping 2124 --- it. The `ipc` argument is a boolean to indicate if this pipe will be used for 2125 --- handle passing between processes. 2126 --- @param ipc boolean? 2127 --- @return uv.uv_pipe_t? pipe 2128 --- @return string? err 2129 --- @return uv.error_name? err_name 2130 function uv.new_pipe(ipc) end 2131 2132 --- Open an existing file descriptor or [`uv_handle_t`][] as a pipe. 2133 --- **Note**: 2134 --- The file descriptor is set to non-blocking mode. 2135 --- @param pipe uv.uv_pipe_t 2136 --- @param fd integer 2137 --- @return 0? success 2138 --- @return string? err 2139 --- @return uv.error_name? err_name 2140 function uv.pipe_open(pipe, fd) end 2141 2142 --- Open an existing file descriptor or [`uv_handle_t`][] as a pipe. 2143 --- **Note**: 2144 --- The file descriptor is set to non-blocking mode. 2145 --- @param fd integer 2146 --- @return 0? success 2147 --- @return string? err 2148 --- @return uv.error_name? err_name 2149 function uv_pipe_t:open(fd) end 2150 2151 --- Bind the pipe to a file path (Unix) or a name (Windows). 2152 --- **Note**: 2153 --- Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2154 --- typically between 92 and 108 bytes. 2155 --- @param pipe uv.uv_pipe_t 2156 --- @param name string 2157 --- @return 0? success 2158 --- @return string? err 2159 --- @return uv.error_name? err_name 2160 function uv.pipe_bind(pipe, name) end 2161 2162 --- Bind the pipe to a file path (Unix) or a name (Windows). 2163 --- **Note**: 2164 --- Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2165 --- typically between 92 and 108 bytes. 2166 --- @param name string 2167 --- @return 0? success 2168 --- @return string? err 2169 --- @return uv.error_name? err_name 2170 function uv_pipe_t:bind(name) end 2171 2172 --- Connect to the Unix domain socket or the named pipe. 2173 --- **Note**: 2174 --- Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2175 --- typically between 92 and 108 bytes. 2176 --- @param pipe uv.uv_pipe_t 2177 --- @param name string 2178 --- @param callback fun(err: string?)? 2179 --- @return uv.uv_connect_t? connect 2180 --- @return string? err 2181 --- @return uv.error_name? err_name 2182 function uv.pipe_connect(pipe, name, callback) end 2183 2184 --- Connect to the Unix domain socket or the named pipe. 2185 --- **Note**: 2186 --- Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2187 --- typically between 92 and 108 bytes. 2188 --- @param name string 2189 --- @param callback fun(err: string?)? 2190 --- @return uv.uv_connect_t? connect 2191 --- @return string? err 2192 --- @return uv.error_name? err_name 2193 function uv_pipe_t:connect(name, callback) end 2194 2195 --- Get the name of the Unix domain socket or the named pipe. 2196 --- @param pipe uv.uv_pipe_t 2197 --- @return string? name 2198 --- @return string? err 2199 --- @return uv.error_name? err_name 2200 function uv.pipe_getsockname(pipe) end 2201 2202 --- Get the name of the Unix domain socket or the named pipe. 2203 --- @return string? name 2204 --- @return string? err 2205 --- @return uv.error_name? err_name 2206 function uv_pipe_t:getsockname() end 2207 2208 --- Get the name of the Unix domain socket or the named pipe to which the handle is 2209 --- connected. 2210 --- @param pipe uv.uv_pipe_t 2211 --- @return string? name 2212 --- @return string? err 2213 --- @return uv.error_name? err_name 2214 function uv.pipe_getpeername(pipe) end 2215 2216 --- Get the name of the Unix domain socket or the named pipe to which the handle is 2217 --- connected. 2218 --- @return string? name 2219 --- @return string? err 2220 --- @return uv.error_name? err_name 2221 function uv_pipe_t:getpeername() end 2222 2223 --- Set the number of pending pipe instance handles when the pipe server is waiting 2224 --- for connections. 2225 --- **Note**: 2226 --- This setting applies to Windows only. 2227 --- @param pipe uv.uv_pipe_t 2228 --- @param count integer 2229 function uv.pipe_pending_instances(pipe, count) end 2230 2231 --- Set the number of pending pipe instance handles when the pipe server is waiting 2232 --- for connections. 2233 --- **Note**: 2234 --- This setting applies to Windows only. 2235 --- @param count integer 2236 function uv_pipe_t:pending_instances(count) end 2237 2238 --- Returns the pending pipe count for the named pipe. 2239 --- @param pipe uv.uv_pipe_t 2240 --- @return integer 2241 function uv.pipe_pending_count(pipe) end 2242 2243 --- Returns the pending pipe count for the named pipe. 2244 --- @return integer 2245 function uv_pipe_t:pending_count() end 2246 2247 --- Used to receive handles over IPC pipes. 2248 --- 2249 --- First - call `uv.pipe_pending_count()`, if it's > 0 then initialize a handle of 2250 --- the given type, returned by `uv.pipe_pending_type()` and call 2251 --- `uv.accept(pipe, handle)`. 2252 --- @param pipe uv.uv_pipe_t 2253 --- @return string 2254 function uv.pipe_pending_type(pipe) end 2255 2256 --- Used to receive handles over IPC pipes. 2257 --- 2258 --- First - call `uv.pipe_pending_count()`, if it's > 0 then initialize a handle of 2259 --- the given type, returned by `uv.pipe_pending_type()` and call 2260 --- `uv.accept(pipe, handle)`. 2261 --- @return string 2262 function uv_pipe_t:pending_type() end 2263 2264 --- Alters pipe permissions, allowing it to be accessed from processes run by different users. 2265 --- Makes the pipe writable or readable by all users. `flags` are: `"r"`, `"w"`, `"rw"`, or `"wr"` 2266 --- where `r` is `READABLE` and `w` is `WRITABLE`. This function is blocking. 2267 --- @param pipe uv.uv_pipe_t 2268 --- @param flags string 2269 --- @return 0? success 2270 --- @return string? err 2271 --- @return uv.error_name? err_name 2272 function uv.pipe_chmod(pipe, flags) end 2273 2274 --- Alters pipe permissions, allowing it to be accessed from processes run by different users. 2275 --- Makes the pipe writable or readable by all users. `flags` are: `"r"`, `"w"`, `"rw"`, or `"wr"` 2276 --- where `r` is `READABLE` and `w` is `WRITABLE`. This function is blocking. 2277 --- @param flags string 2278 --- @return 0? success 2279 --- @return string? err 2280 --- @return uv.error_name? err_name 2281 function uv_pipe_t:chmod(flags) end 2282 2283 --- @class uv.pipe.fds 2284 --- 2285 --- (file descriptor) 2286 --- @field read integer 2287 --- 2288 --- (file descriptor) 2289 --- @field write integer 2290 2291 --- Create a pair of connected pipe handles. Data may be written to the `write` fd and read from the `read` fd. The resulting handles can be passed to `pipe_open`, used with `spawn`, or for any other purpose. 2292 --- 2293 --- Flags: 2294 --- - `nonblock`: Opens the specified socket handle for `OVERLAPPED` or `FIONBIO`/`O_NONBLOCK` I/O usage. This is recommended for handles that will be used by libuv, and not usually recommended otherwise. 2295 --- 2296 --- Equivalent to `pipe(2)` with the `O_CLOEXEC` flag set. 2297 --- Example 2298 --- ```lua 2299 --- -- Simple read/write with pipe_open 2300 --- local fds = uv.pipe({nonblock=true}, {nonblock=true}) 2301 --- 2302 --- local read_pipe = uv.new_pipe() 2303 --- read_pipe:open(fds.read) 2304 --- 2305 --- local write_pipe = uv.new_pipe() 2306 --- write_pipe:open(fds.write) 2307 --- 2308 --- write_pipe:write("hello") 2309 --- read_pipe:read_start(function(err, chunk) 2310 --- assert(not err, err) 2311 --- print(chunk) 2312 --- end) 2313 --- ``` 2314 --- @param read_flags { nonblock: boolean }? 2315 --- @param write_flags { nonblock: boolean }? 2316 --- @return uv.pipe.fds? fds 2317 --- @return string? err 2318 --- @return uv.error_name? err_name 2319 function uv.pipe(read_flags, write_flags) end 2320 2321 --- Bind the pipe to a file path (Unix) or a name (Windows). 2322 --- 2323 --- `Flags`: 2324 --- 2325 --- - If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. 2326 --- - If `type(flags)` is `table`, it must be `{}` or `{ no_truncate = true|false }`. 2327 --- - If `type(flags)` is `nil`, it use default value `0`. 2328 --- - Returns `EINVAL` for unsupported flags without performing the bind operation. 2329 --- 2330 --- Supports Linux abstract namespace sockets. namelen must include the leading '\0' byte but not the trailing nul byte. 2331 --- **Note**: 2332 --- 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2333 --- typically between 92 and 108 bytes. 2334 --- 2. New in version 1.46.0. 2335 --- @param pipe uv.uv_pipe_t 2336 --- @param name string 2337 --- @param flags integer|table? 2338 --- @return 0? success 2339 --- @return string? err 2340 --- @return uv.error_name? err_name 2341 function uv.pipe_bind2(pipe, name, flags) end 2342 2343 --- Bind the pipe to a file path (Unix) or a name (Windows). 2344 --- 2345 --- `Flags`: 2346 --- 2347 --- - If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. 2348 --- - If `type(flags)` is `table`, it must be `{}` or `{ no_truncate = true|false }`. 2349 --- - If `type(flags)` is `nil`, it use default value `0`. 2350 --- - Returns `EINVAL` for unsupported flags without performing the bind operation. 2351 --- 2352 --- Supports Linux abstract namespace sockets. namelen must include the leading '\0' byte but not the trailing nul byte. 2353 --- **Note**: 2354 --- 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2355 --- typically between 92 and 108 bytes. 2356 --- 2. New in version 1.46.0. 2357 --- @param name string 2358 --- @param flags integer|table? 2359 --- @return 0? success 2360 --- @return string? err 2361 --- @return uv.error_name? err_name 2362 function uv_pipe_t:bind2(name, flags) end 2363 2364 --- Connect to the Unix domain socket or the named pipe. 2365 --- 2366 --- `Flags`: 2367 --- 2368 --- - If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. 2369 --- - If `type(flags)` is `table`, it must be `{}` or `{ no_truncate = true|false }`. 2370 --- - If `type(flags)` is `nil`, it use default value `0`. 2371 --- - Returns `EINVAL` for unsupported flags without performing the bind operation. 2372 --- 2373 --- Supports Linux abstract namespace sockets. namelen must include the leading nul byte but not the trailing nul byte. 2374 --- **Note**: 2375 --- 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2376 --- typically between 92 and 108 bytes. 2377 --- 2. New in version 1.46.0. 2378 --- @param pipe uv.uv_pipe_t 2379 --- @param name string 2380 --- @param flags integer|table? 2381 --- @param callback fun(err: string?)? 2382 --- @return uv.uv_connect_t? connect 2383 --- @return string? err 2384 --- @return uv.error_name? err_name 2385 function uv.pipe_connect2(pipe, name, flags, callback) end 2386 2387 --- Connect to the Unix domain socket or the named pipe. 2388 --- 2389 --- `Flags`: 2390 --- 2391 --- - If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. 2392 --- - If `type(flags)` is `table`, it must be `{}` or `{ no_truncate = true|false }`. 2393 --- - If `type(flags)` is `nil`, it use default value `0`. 2394 --- - Returns `EINVAL` for unsupported flags without performing the bind operation. 2395 --- 2396 --- Supports Linux abstract namespace sockets. namelen must include the leading nul byte but not the trailing nul byte. 2397 --- **Note**: 2398 --- 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, 2399 --- typically between 92 and 108 bytes. 2400 --- 2. New in version 1.46.0. 2401 --- @param name string 2402 --- @param flags integer|table? 2403 --- @param callback fun(err: string?)? 2404 --- @return uv.uv_connect_t? connect 2405 --- @return string? err 2406 --- @return uv.error_name? err_name 2407 function uv_pipe_t:connect2(name, flags, callback) end 2408 2409 2410 --- # `uv_tty_t` - TTY handle 2411 --- 2412 --- > [`uv_handle_t`][] and [`uv_stream_t`][] functions also apply. 2413 --- 2414 --- TTY handles represent a stream for the console. 2415 --- 2416 --- ```lua 2417 --- -- Simple echo program 2418 --- local stdin = uv.new_tty(0, true) 2419 --- local stdout = uv.new_tty(1, false) 2420 --- 2421 --- stdin:read_start(function (err, data) 2422 --- assert(not err, err) 2423 --- if data then 2424 --- stdout:write(data) 2425 --- else 2426 --- stdin:close() 2427 --- stdout:close() 2428 --- end 2429 --- end) 2430 --- ``` 2431 --- @class uv.uv_tty_t : uv.uv_stream_t 2432 local uv_tty_t = {} 2433 2434 --- Initialize a new TTY stream with the given file descriptor. Usually the file 2435 --- descriptor will be: 2436 --- 2437 --- - 0 - stdin 2438 --- - 1 - stdout 2439 --- - 2 - stderr 2440 --- 2441 --- On Unix this function will determine the path of the fd of the terminal using 2442 --- ttyname_r(3), open it, and use it if the passed file descriptor refers to a TTY. 2443 --- This lets libuv put the tty in non-blocking mode without affecting other 2444 --- processes that share the tty. 2445 --- 2446 --- This function is not thread safe on systems that don’t support ioctl TIOCGPTN or TIOCPTYGNAME, for instance OpenBSD and Solaris. 2447 --- **Note**: 2448 --- If reopening the TTY fails, libuv falls back to blocking writes. 2449 --- @param fd integer 2450 --- @param readable boolean 2451 --- @return uv.uv_tty_t? tty 2452 --- @return string? err 2453 --- @return uv.error_name? err_name 2454 function uv.new_tty(fd, readable) end 2455 2456 --- Set the TTY using the specified terminal mode. 2457 --- 2458 --- See [Constants][] for supported TTY mode input values. 2459 --- @param tty uv.uv_tty_t 2460 --- @param mode string|integer 2461 --- @return 0? success 2462 --- @return string? err 2463 --- @return uv.error_name? err_name 2464 function uv.tty_set_mode(tty, mode) end 2465 2466 --- Set the TTY using the specified terminal mode. 2467 --- 2468 --- See [Constants][] for supported TTY mode input values. 2469 --- @param mode string|integer 2470 --- @return 0? success 2471 --- @return string? err 2472 --- @return uv.error_name? err_name 2473 function uv_tty_t:set_mode(mode) end 2474 2475 --- To be called when the program exits. Resets TTY settings to default values for 2476 --- the next process to take over. 2477 --- 2478 --- This function is async signal-safe on Unix platforms but can fail with error 2479 --- code `EBUSY` if you call it when execution is inside `uv.tty_set_mode()`. 2480 --- @return 0? success 2481 --- @return string? err 2482 --- @return uv.error_name? err_name 2483 function uv.tty_reset_mode() end 2484 2485 --- Gets the current Window width and height. 2486 --- @param tty uv.uv_tty_t 2487 --- @return integer? width 2488 --- @return integer|string height_or_err 2489 --- @return uv.error_name? err_name 2490 function uv.tty_get_winsize(tty) end 2491 2492 --- Gets the current Window width and height. 2493 --- @return integer? width 2494 --- @return integer|string height_or_err 2495 --- @return uv.error_name? err_name 2496 function uv_tty_t:get_winsize() end 2497 2498 --- Controls whether console virtual terminal sequences are processed by libuv or 2499 --- console. Useful in particular for enabling ConEmu support of ANSI X3.64 and 2500 --- Xterm 256 colors. Otherwise Windows10 consoles are usually detected 2501 --- automatically. State should be one of: `"supported"` or `"unsupported"`. 2502 --- 2503 --- This function is only meaningful on Windows systems. On Unix it is silently 2504 --- ignored. 2505 --- @param state string 2506 function uv.tty_set_vterm_state(state) end 2507 2508 --- Get the current state of whether console virtual terminal sequences are handled 2509 --- by libuv or the console. The return value is `"supported"` or `"unsupported"`. 2510 --- 2511 --- This function is not implemented on Unix, where it returns `ENOTSUP`. 2512 --- @return string? state 2513 --- @return string? err 2514 --- @return uv.error_name? err_name 2515 function uv.tty_get_vterm_state() end 2516 2517 2518 --- # `uv_udp_t` - UDP handle 2519 --- 2520 --- > [`uv_handle_t`][] functions also apply. 2521 --- 2522 --- UDP handles encapsulate UDP communication for both clients and servers. 2523 --- @class uv.uv_udp_t : uv.uv_handle_t 2524 local uv_udp_t = {} 2525 2526 --- Creates and initializes a new `uv_udp_t`. Returns the Lua userdata wrapping 2527 --- it. The actual socket is created lazily. 2528 --- 2529 --- See [Constants][] for supported address `family` input values. 2530 --- 2531 --- When specified, `mmsgs` determines the number of messages able to be received 2532 --- at one time via `recvmmsg(2)` (the allocated buffer will be sized to be able 2533 --- to fit the specified number of max size dgrams). Only has an effect on 2534 --- platforms that support `recvmmsg(2)`. 2535 --- 2536 --- **Note:** For backwards compatibility reasons, `flags` can also be a string or 2537 --- integer. When it is a string, it will be treated like the `family` key above. 2538 --- When it is an integer, it will be used directly as the `flags` parameter when 2539 --- calling `uv_udp_init_ex`. 2540 --- @param flags { family: string?, mmsgs: integer? }? 2541 --- @return uv.uv_udp_t? udp 2542 --- @return string? err 2543 --- @return uv.error_name? err_name 2544 function uv.new_udp(flags) end 2545 2546 --- Returns the handle's send queue size. 2547 --- @param udp uv.uv_udp_t 2548 --- @return integer 2549 function uv.udp_get_send_queue_size(udp) end 2550 2551 --- Returns the handle's send queue size. 2552 --- @return integer 2553 function uv_udp_t:get_send_queue_size() end 2554 2555 --- Returns the handle's send queue count. 2556 --- @param udp uv.uv_udp_t 2557 --- @return integer 2558 function uv.udp_get_send_queue_count(udp) end 2559 2560 --- Returns the handle's send queue count. 2561 --- @return integer 2562 function uv_udp_t:get_send_queue_count() end 2563 2564 --- Opens an existing file descriptor or Windows SOCKET as a UDP handle. 2565 --- 2566 --- Unix only: The only requirement of the sock argument is that it follows the 2567 --- datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(), 2568 --- etc). In other words, other datagram-type sockets like raw sockets or netlink 2569 --- sockets can also be passed to this function. 2570 --- 2571 --- The file descriptor is set to non-blocking mode. 2572 --- 2573 --- Note: The passed file descriptor or SOCKET is not checked for its type, but 2574 --- it's required that it represents a valid datagram socket. 2575 --- @param udp uv.uv_udp_t 2576 --- @param fd integer 2577 --- @return 0? success 2578 --- @return string? err 2579 --- @return uv.error_name? err_name 2580 function uv.udp_open(udp, fd) end 2581 2582 --- Opens an existing file descriptor or Windows SOCKET as a UDP handle. 2583 --- 2584 --- Unix only: The only requirement of the sock argument is that it follows the 2585 --- datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(), 2586 --- etc). In other words, other datagram-type sockets like raw sockets or netlink 2587 --- sockets can also be passed to this function. 2588 --- 2589 --- The file descriptor is set to non-blocking mode. 2590 --- 2591 --- Note: The passed file descriptor or SOCKET is not checked for its type, but 2592 --- it's required that it represents a valid datagram socket. 2593 --- @param fd integer 2594 --- @return 0? success 2595 --- @return string? err 2596 --- @return uv.error_name? err_name 2597 function uv_udp_t:open(fd) end 2598 2599 --- Bind the UDP handle to an IP address and port. Any `flags` are set with a table 2600 --- with fields `reuseaddr` or `ipv6only` equal to `true` or `false`. 2601 --- @param udp uv.uv_udp_t 2602 --- @param host string 2603 --- @param port number 2604 --- @param flags { ipv6only: boolean?, reuseaddr: boolean? }? 2605 --- @return 0? success 2606 --- @return string? err 2607 --- @return uv.error_name? err_name 2608 function uv.udp_bind(udp, host, port, flags) end 2609 2610 --- Bind the UDP handle to an IP address and port. Any `flags` are set with a table 2611 --- with fields `reuseaddr` or `ipv6only` equal to `true` or `false`. 2612 --- @param host string 2613 --- @param port number 2614 --- @param flags { ipv6only: boolean?, reuseaddr: boolean? }? 2615 --- @return 0? success 2616 --- @return string? err 2617 --- @return uv.error_name? err_name 2618 function uv_udp_t:bind(host, port, flags) end 2619 2620 --- Get the local IP and port of the UDP handle. 2621 --- @param udp uv.uv_udp_t 2622 --- @return uv.socketinfo? address 2623 --- @return string? err 2624 --- @return uv.error_name? err_name 2625 function uv.udp_getsockname(udp) end 2626 2627 --- Get the local IP and port of the UDP handle. 2628 --- @return uv.socketinfo? address 2629 --- @return string? err 2630 --- @return uv.error_name? err_name 2631 function uv_udp_t:getsockname() end 2632 2633 --- Get the remote IP and port of the UDP handle on connected UDP handles. 2634 --- @param udp uv.uv_udp_t 2635 --- @return uv.socketinfo? address 2636 --- @return string? err 2637 --- @return uv.error_name? err_name 2638 function uv.udp_getpeername(udp) end 2639 2640 --- Get the remote IP and port of the UDP handle on connected UDP handles. 2641 --- @return uv.socketinfo? address 2642 --- @return string? err 2643 --- @return uv.error_name? err_name 2644 function uv_udp_t:getpeername() end 2645 2646 --- Set membership for a multicast address. `multicast_addr` is multicast address to 2647 --- set membership for. `interface_addr` is interface address. `membership` can be 2648 --- the string `"leave"` or `"join"`. 2649 --- @param udp uv.uv_udp_t 2650 --- @param multicast_addr string 2651 --- @param interface_addr string? 2652 --- @param membership string 2653 --- @return 0? success 2654 --- @return string? err 2655 --- @return uv.error_name? err_name 2656 function uv.udp_set_membership(udp, multicast_addr, interface_addr, membership) end 2657 2658 --- Set membership for a multicast address. `multicast_addr` is multicast address to 2659 --- set membership for. `interface_addr` is interface address. `membership` can be 2660 --- the string `"leave"` or `"join"`. 2661 --- @param multicast_addr string 2662 --- @param interface_addr string? 2663 --- @param membership string 2664 --- @return 0? success 2665 --- @return string? err 2666 --- @return uv.error_name? err_name 2667 function uv_udp_t:set_membership(multicast_addr, interface_addr, membership) end 2668 2669 --- Set membership for a source-specific multicast group. `multicast_addr` is multicast 2670 --- address to set membership for. `interface_addr` is interface address. `source_addr` 2671 --- is source address. `membership` can be the string `"leave"` or `"join"`. 2672 --- @param udp uv.uv_udp_t 2673 --- @param multicast_addr string 2674 --- @param interface_addr string? 2675 --- @param source_addr string 2676 --- @param membership string 2677 --- @return 0? success 2678 --- @return string? err 2679 --- @return uv.error_name? err_name 2680 function uv.udp_set_source_membership(udp, multicast_addr, interface_addr, source_addr, membership) end 2681 2682 --- Set membership for a source-specific multicast group. `multicast_addr` is multicast 2683 --- address to set membership for. `interface_addr` is interface address. `source_addr` 2684 --- is source address. `membership` can be the string `"leave"` or `"join"`. 2685 --- @param multicast_addr string 2686 --- @param interface_addr string? 2687 --- @param source_addr string 2688 --- @param membership string 2689 --- @return 0? success 2690 --- @return string? err 2691 --- @return uv.error_name? err_name 2692 function uv_udp_t:set_source_membership(multicast_addr, interface_addr, source_addr, membership) end 2693 2694 --- Set IP multicast loop flag. Makes multicast packets loop back to local 2695 --- sockets. 2696 --- @param udp uv.uv_udp_t 2697 --- @param on boolean 2698 --- @return 0? success 2699 --- @return string? err 2700 --- @return uv.error_name? err_name 2701 function uv.udp_set_multicast_loop(udp, on) end 2702 2703 --- Set IP multicast loop flag. Makes multicast packets loop back to local 2704 --- sockets. 2705 --- @param on boolean 2706 --- @return 0? success 2707 --- @return string? err 2708 --- @return uv.error_name? err_name 2709 function uv_udp_t:set_multicast_loop(on) end 2710 2711 --- Set the multicast ttl. 2712 --- 2713 --- `ttl` is an integer 1 through 255. 2714 --- @param udp uv.uv_udp_t 2715 --- @param ttl integer 2716 --- @return 0? success 2717 --- @return string? err 2718 --- @return uv.error_name? err_name 2719 function uv.udp_set_multicast_ttl(udp, ttl) end 2720 2721 --- Set the multicast ttl. 2722 --- 2723 --- `ttl` is an integer 1 through 255. 2724 --- @param ttl integer 2725 --- @return 0? success 2726 --- @return string? err 2727 --- @return uv.error_name? err_name 2728 function uv_udp_t:set_multicast_ttl(ttl) end 2729 2730 --- Set the multicast interface to send or receive data on. 2731 --- @param udp uv.uv_udp_t 2732 --- @param interface_addr string 2733 --- @return 0? success 2734 --- @return string? err 2735 --- @return uv.error_name? err_name 2736 function uv.udp_set_multicast_interface(udp, interface_addr) end 2737 2738 --- Set the multicast interface to send or receive data on. 2739 --- @param interface_addr string 2740 --- @return 0? success 2741 --- @return string? err 2742 --- @return uv.error_name? err_name 2743 function uv_udp_t:set_multicast_interface(interface_addr) end 2744 2745 --- Set broadcast on or off. 2746 --- @param udp uv.uv_udp_t 2747 --- @param on boolean 2748 --- @return 0? success 2749 --- @return string? err 2750 --- @return uv.error_name? err_name 2751 function uv.udp_set_broadcast(udp, on) end 2752 2753 --- Set broadcast on or off. 2754 --- @param on boolean 2755 --- @return 0? success 2756 --- @return string? err 2757 --- @return uv.error_name? err_name 2758 function uv_udp_t:set_broadcast(on) end 2759 2760 --- Set the time to live. 2761 --- 2762 --- `ttl` is an integer 1 through 255. 2763 --- @param udp uv.uv_udp_t 2764 --- @param ttl integer 2765 --- @return 0? success 2766 --- @return string? err 2767 --- @return uv.error_name? err_name 2768 function uv.udp_set_ttl(udp, ttl) end 2769 2770 --- Set the time to live. 2771 --- 2772 --- `ttl` is an integer 1 through 255. 2773 --- @param ttl integer 2774 --- @return 0? success 2775 --- @return string? err 2776 --- @return uv.error_name? err_name 2777 function uv_udp_t:set_ttl(ttl) end 2778 2779 --- Send data over the UDP socket. If the socket has not previously been bound 2780 --- with `uv.udp_bind()` it will be bound to `0.0.0.0` (the "all interfaces" IPv4 2781 --- address) and a random port number. 2782 --- @param udp uv.uv_udp_t 2783 --- @param data uv.buffer 2784 --- @param host string 2785 --- @param port integer 2786 --- @param callback fun(err: string?) 2787 --- @return uv.uv_udp_send_t? send 2788 --- @return string? err 2789 --- @return uv.error_name? err_name 2790 function uv.udp_send(udp, data, host, port, callback) end 2791 2792 --- Send data over the UDP socket. If the socket has not previously been bound 2793 --- with `uv.udp_bind()` it will be bound to `0.0.0.0` (the "all interfaces" IPv4 2794 --- address) and a random port number. 2795 --- @param data uv.buffer 2796 --- @param host string 2797 --- @param port integer 2798 --- @param callback fun(err: string?) 2799 --- @return uv.uv_udp_send_t? send 2800 --- @return string? err 2801 --- @return uv.error_name? err_name 2802 function uv_udp_t:send(data, host, port, callback) end 2803 2804 --- Same as `uv.udp_send()`, but won't queue a send request if it can't be 2805 --- completed immediately. 2806 --- @param udp uv.uv_udp_t 2807 --- @param data uv.buffer 2808 --- @param host string 2809 --- @param port integer 2810 --- @return integer? bytes_sent 2811 --- @return string? err 2812 --- @return uv.error_name? err_name 2813 function uv.udp_try_send(udp, data, host, port) end 2814 2815 --- Same as `uv.udp_send()`, but won't queue a send request if it can't be 2816 --- completed immediately. 2817 --- @param data uv.buffer 2818 --- @param host string 2819 --- @param port integer 2820 --- @return integer? bytes_sent 2821 --- @return string? err 2822 --- @return uv.error_name? err_name 2823 function uv_udp_t:try_send(data, host, port) end 2824 2825 --- Like `uv.udp_try_send()`, but can send multiple datagrams. 2826 --- Lightweight abstraction around `sendmmsg(2)`, with a `sendmsg(2)` fallback loop 2827 --- for platforms that do not support the former. The `udp` handle must be fully 2828 --- initialized, either from a `uv.udp_bind` call, another call that will bind 2829 --- automatically (`udp_send`, `udp_try_send`, etc), or from `uv.udp_connect`. 2830 --- 2831 --- `messages` should be an array-like table, where `addr` must be specified 2832 --- if the `udp` has not been connected via `udp_connect`. Otherwise, `addr` 2833 --- must be `nil`. 2834 --- 2835 --- `flags` is reserved for future extension and must currently be `nil` or `0` or 2836 --- `{}`. 2837 --- 2838 --- Returns the number of messages sent successfully. An error will only be returned 2839 --- if the first datagram failed to be sent. 2840 --- Example 2841 --- ```lua 2842 --- -- If client:connect(...) was not called 2843 --- local addr = { ip = "127.0.0.1", port = 1234 } 2844 --- client:try_send2({ 2845 --- { data = "Message 1", addr = addr }, 2846 --- { data = "Message 2", addr = addr }, 2847 --- }) 2848 --- 2849 --- -- If client:connect(...) was called 2850 --- client:try_send2({ 2851 --- { data = "Message 1" }, 2852 --- { data = "Message 2" }, 2853 --- }) 2854 --- ``` 2855 --- @param udp uv.uv_udp_t 2856 --- @param messages table<integer, { data: uv.buffer, addr: { ip: string, port: integer } }> 2857 --- @param flags 0|{}? 2858 --- @param port integer 2859 --- @return integer? messages_sent 2860 --- @return string? err 2861 --- @return uv.error_name? err_name 2862 function uv.udp_try_send2(udp, messages, flags, port) end 2863 2864 --- Like `uv.udp_try_send()`, but can send multiple datagrams. 2865 --- Lightweight abstraction around `sendmmsg(2)`, with a `sendmsg(2)` fallback loop 2866 --- for platforms that do not support the former. The `udp` handle must be fully 2867 --- initialized, either from a `uv.udp_bind` call, another call that will bind 2868 --- automatically (`udp_send`, `udp_try_send`, etc), or from `uv.udp_connect`. 2869 --- 2870 --- `messages` should be an array-like table, where `addr` must be specified 2871 --- if the `udp` has not been connected via `udp_connect`. Otherwise, `addr` 2872 --- must be `nil`. 2873 --- 2874 --- `flags` is reserved for future extension and must currently be `nil` or `0` or 2875 --- `{}`. 2876 --- 2877 --- Returns the number of messages sent successfully. An error will only be returned 2878 --- if the first datagram failed to be sent. 2879 --- Example 2880 --- ```lua 2881 --- -- If client:connect(...) was not called 2882 --- local addr = { ip = "127.0.0.1", port = 1234 } 2883 --- client:try_send2({ 2884 --- { data = "Message 1", addr = addr }, 2885 --- { data = "Message 2", addr = addr }, 2886 --- }) 2887 --- 2888 --- -- If client:connect(...) was called 2889 --- client:try_send2({ 2890 --- { data = "Message 1" }, 2891 --- { data = "Message 2" }, 2892 --- }) 2893 --- ``` 2894 --- @param messages table<integer, { data: uv.buffer, addr: { ip: string, port: integer } }> 2895 --- @param flags 0|{}? 2896 --- @param port integer 2897 --- @return integer? messages_sent 2898 --- @return string? err 2899 --- @return uv.error_name? err_name 2900 function uv_udp_t:try_send2(messages, flags, port) end 2901 2902 --- @alias uv.udp_recv_start.callback 2903 --- | fun(err: string?, data: string?, addr: uv.udp_recv_start.callback.addr?, flags: { partial: boolean?, mmsg_chunk: boolean? }) 2904 2905 --- @class uv.udp_recv_start.callback.addr 2906 --- @field ip string 2907 --- @field port integer 2908 --- @field family string 2909 2910 --- Prepare for receiving data. If the socket has not previously been bound with 2911 --- `uv.udp_bind()` it is bound to `0.0.0.0` (the "all interfaces" IPv4 address) 2912 --- and a random port number. 2913 --- 2914 --- See [Constants][] for supported address `family` output values. 2915 --- @param udp uv.uv_udp_t 2916 --- @param callback uv.udp_recv_start.callback 2917 --- @return 0? success 2918 --- @return string? err 2919 --- @return uv.error_name? err_name 2920 function uv.udp_recv_start(udp, callback) end 2921 2922 --- Prepare for receiving data. If the socket has not previously been bound with 2923 --- `uv.udp_bind()` it is bound to `0.0.0.0` (the "all interfaces" IPv4 address) 2924 --- and a random port number. 2925 --- 2926 --- See [Constants][] for supported address `family` output values. 2927 --- @param callback uv.udp_recv_start.callback 2928 --- @return 0? success 2929 --- @return string? err 2930 --- @return uv.error_name? err_name 2931 function uv_udp_t:recv_start(callback) end 2932 2933 --- Stop listening for incoming datagrams. 2934 --- @param udp uv.uv_udp_t 2935 --- @return 0? success 2936 --- @return string? err 2937 --- @return uv.error_name? err_name 2938 function uv.udp_recv_stop(udp) end 2939 2940 --- Stop listening for incoming datagrams. 2941 --- @return 0? success 2942 --- @return string? err 2943 --- @return uv.error_name? err_name 2944 function uv_udp_t:recv_stop() end 2945 2946 --- Associate the UDP handle to a remote address and port, so every message sent by 2947 --- this handle is automatically sent to that destination. Calling this function 2948 --- with a NULL addr disconnects the handle. Trying to call `uv.udp_connect()` on an 2949 --- already connected handle will result in an `EISCONN` error. Trying to disconnect 2950 --- a handle that is not connected will return an `ENOTCONN` error. 2951 --- @param udp uv.uv_udp_t 2952 --- @param host string 2953 --- @param port integer 2954 --- @return 0? success 2955 --- @return string? err 2956 --- @return uv.error_name? err_name 2957 function uv.udp_connect(udp, host, port) end 2958 2959 --- Associate the UDP handle to a remote address and port, so every message sent by 2960 --- this handle is automatically sent to that destination. Calling this function 2961 --- with a NULL addr disconnects the handle. Trying to call `uv.udp_connect()` on an 2962 --- already connected handle will result in an `EISCONN` error. Trying to disconnect 2963 --- a handle that is not connected will return an `ENOTCONN` error. 2964 --- @param host string 2965 --- @param port integer 2966 --- @return 0? success 2967 --- @return string? err 2968 --- @return uv.error_name? err_name 2969 function uv_udp_t:connect(host, port) end 2970 2971 2972 --- # `uv_fs_event_t` - FS Event handle 2973 --- 2974 --- > [`uv_handle_t`][] functions also apply. 2975 --- 2976 --- FS Event handles allow the user to monitor a given path for changes, for 2977 --- example, if the file was renamed or there was a generic change in it. This 2978 --- handle uses the best backend for the job on each platform. 2979 --- @class uv.uv_fs_event_t : uv.uv_handle_t 2980 local uv_fs_event_t = {} 2981 2982 --- Creates and initializes a new `uv_fs_event_t`. Returns the Lua userdata wrapping 2983 --- it. 2984 --- @return uv.uv_fs_event_t? fs_event 2985 --- @return string? err 2986 --- @return uv.error_name? err_name 2987 function uv.new_fs_event() end 2988 2989 --- @alias uv.fs_event_start.callback 2990 --- | fun(err: string?, filename: string, events: { change: boolean?, rename: boolean? }) 2991 2992 --- @class uv.fs_event_start.flags 2993 --- @field watch_entry boolean? 2994 --- @field stat boolean? 2995 --- @field recursive boolean? 2996 2997 --- Start the handle with the given callback, which will watch the specified path 2998 --- for changes. 2999 --- @param fs_event uv.uv_fs_event_t 3000 --- @param path string 3001 --- @param flags uv.fs_event_start.flags 3002 --- @param callback uv.fs_event_start.callback 3003 --- @return 0? success 3004 --- @return string? err 3005 --- @return uv.error_name? err_name 3006 function uv.fs_event_start(fs_event, path, flags, callback) end 3007 3008 --- Start the handle with the given callback, which will watch the specified path 3009 --- for changes. 3010 --- @param path string 3011 --- @param flags uv.fs_event_start.flags 3012 --- @param callback uv.fs_event_start.callback 3013 --- @return 0? success 3014 --- @return string? err 3015 --- @return uv.error_name? err_name 3016 function uv_fs_event_t:start(path, flags, callback) end 3017 3018 --- Stop the handle, the callback will no longer be called. 3019 --- @param fs_event uv.uv_fs_event_t 3020 --- @return 0? success 3021 --- @return string? err 3022 --- @return uv.error_name? err_name 3023 function uv.fs_event_stop(fs_event) end 3024 3025 --- Stop the handle, the callback will no longer be called. 3026 --- @return 0? success 3027 --- @return string? err 3028 --- @return uv.error_name? err_name 3029 function uv_fs_event_t:stop() end 3030 3031 --- Get the path being monitored by the handle. 3032 --- @param fs_event uv.uv_fs_event_t 3033 --- @return string? path 3034 --- @return string? err 3035 --- @return uv.error_name? err_name 3036 function uv.fs_event_getpath(fs_event) end 3037 3038 --- Get the path being monitored by the handle. 3039 --- @return string? path 3040 --- @return string? err 3041 --- @return uv.error_name? err_name 3042 function uv_fs_event_t:getpath() end 3043 3044 3045 --- # `uv_fs_poll_t` - FS Poll handle 3046 --- 3047 --- > [`uv_handle_t`][] functions also apply. 3048 --- 3049 --- FS Poll handles allow the user to monitor a given path for changes. Unlike 3050 --- `uv_fs_event_t`, fs poll handles use `stat` to detect when a file has changed so 3051 --- they can work on file systems where fs event handles can't. 3052 --- @class uv.uv_fs_poll_t : uv.uv_handle_t 3053 local uv_fs_poll_t = {} 3054 3055 --- Creates and initializes a new `uv_fs_poll_t`. Returns the Lua userdata wrapping 3056 --- it. 3057 --- @return uv.uv_fs_poll_t? fs_poll 3058 --- @return string? err 3059 --- @return uv.error_name? err_name 3060 function uv.new_fs_poll() end 3061 3062 --- @alias uv.fs_poll_start.callback 3063 --- | fun(err: string?, prev: table?, curr: table?) 3064 3065 --- Check the file at `path` for changes every `interval` milliseconds. 3066 --- 3067 --- **Note:** For maximum portability, use multi-second intervals. Sub-second 3068 --- intervals will not detect all changes on many file systems. 3069 --- @param fs_poll uv.uv_fs_poll_t 3070 --- @param path string 3071 --- @param interval integer 3072 --- @param callback uv.fs_poll_start.callback 3073 --- @return 0? success 3074 --- @return string? err 3075 --- @return uv.error_name? err_name 3076 function uv.fs_poll_start(fs_poll, path, interval, callback) end 3077 3078 --- Check the file at `path` for changes every `interval` milliseconds. 3079 --- 3080 --- **Note:** For maximum portability, use multi-second intervals. Sub-second 3081 --- intervals will not detect all changes on many file systems. 3082 --- @param path string 3083 --- @param interval integer 3084 --- @param callback uv.fs_poll_start.callback 3085 --- @return 0? success 3086 --- @return string? err 3087 --- @return uv.error_name? err_name 3088 function uv_fs_poll_t:start(path, interval, callback) end 3089 3090 --- Stop the handle, the callback will no longer be called. 3091 --- @param fs_poll uv.uv_fs_poll_t 3092 --- @return 0? success 3093 --- @return string? err 3094 --- @return uv.error_name? err_name 3095 function uv.fs_poll_stop(fs_poll) end 3096 3097 --- Stop the handle, the callback will no longer be called. 3098 --- @return 0? success 3099 --- @return string? err 3100 --- @return uv.error_name? err_name 3101 function uv_fs_poll_t:stop() end 3102 3103 --- Get the path being monitored by the handle. 3104 --- @param fs_poll uv.uv_fs_poll_t 3105 --- @return string? path 3106 --- @return string? err 3107 --- @return uv.error_name? err_name 3108 function uv.fs_poll_getpath(fs_poll) end 3109 3110 --- Get the path being monitored by the handle. 3111 --- @return string? path 3112 --- @return string? err 3113 --- @return uv.error_name? err_name 3114 function uv_fs_poll_t:getpath() end 3115 3116 3117 --- # File system operations 3118 --- 3119 --- Most file system functions can operate synchronously or asynchronously. When a synchronous version is called (by omitting a callback), the function will 3120 --- immediately return the results of the FS call. When an asynchronous version is 3121 --- called (by providing a callback), the function will immediately return a 3122 --- `uv_fs_t userdata` and asynchronously execute its callback; if an error is encountered, the first and only argument passed to the callback will be the `err` error string; if the operation completes successfully, the first argument will be `nil` and the remaining arguments will be the results of the FS call. 3123 --- 3124 --- Synchronous and asynchronous versions of `readFile` (with naive error handling) 3125 --- are implemented below as an example: 3126 --- 3127 --- ```lua 3128 --- local function readFileSync(path) 3129 --- local fd = assert(uv.fs_open(path, "r", 438)) 3130 --- local stat = assert(uv.fs_fstat(fd)) 3131 --- local data = assert(uv.fs_read(fd, stat.size, 0)) 3132 --- assert(uv.fs_close(fd)) 3133 --- return data 3134 --- end 3135 --- 3136 --- local data = readFileSync("main.lua") 3137 --- print("synchronous read", data) 3138 --- ``` 3139 --- 3140 --- ```lua 3141 --- local function readFile(path, callback) 3142 --- uv.fs_open(path, "r", 438, function(err, fd) 3143 --- assert(not err, err) 3144 --- uv.fs_fstat(fd, function(err, stat) 3145 --- assert(not err, err) 3146 --- uv.fs_read(fd, stat.size, 0, function(err, data) 3147 --- assert(not err, err) 3148 --- uv.fs_close(fd, function(err) 3149 --- assert(not err, err) 3150 --- return callback(data) 3151 --- end) 3152 --- end) 3153 --- end) 3154 --- end) 3155 --- end 3156 --- 3157 --- readFile("main.lua", function(data) 3158 --- print("asynchronous read", data) 3159 --- end) 3160 --- ``` 3161 3162 --- Equivalent to `close(2)`. 3163 --- @param fd integer 3164 --- @return boolean? success 3165 --- @return string? err 3166 --- @return uv.error_name? err_name 3167 --- @overload fun(fd: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3168 function uv.fs_close(fd) end 3169 3170 --- Equivalent to `open(2)`. Access `flags` may be an integer or one of: `"r"`, 3171 --- `"rs"`, `"sr"`, `"r+"`, `"rs+"`, `"sr+"`, `"w"`, `"wx"`, `"xw"`, `"w+"`, 3172 --- `"wx+"`, `"xw+"`, `"a"`, `"ax"`, `"xa"`, `"a+"`, `"ax+"`, or "`xa+`". 3173 --- **Note**: 3174 --- On Windows, libuv uses `CreateFileW` and thus the file is always 3175 --- opened in binary mode. Because of this, the `O_BINARY` and `O_TEXT` flags are 3176 --- not supported. 3177 --- @param path string 3178 --- @param flags string|integer 3179 --- @param mode integer (octal `chmod(1)` mode, e.g. `tonumber('644', 8)`) 3180 --- @return integer? fd 3181 --- @return string? err 3182 --- @return uv.error_name? err_name 3183 --- @overload fun(path: string, flags: string|integer, mode: integer, callback: fun(err: string?, fd: integer?)): uv.uv_fs_t 3184 function uv.fs_open(path, flags, mode) end 3185 3186 --- Equivalent to `preadv(2)`. Returns any data. An empty string indicates EOF. 3187 --- 3188 --- If `offset` is nil or omitted, it will default to `-1`, which indicates 'use and update the current file offset.' 3189 --- 3190 --- **Note:** When `offset` is >= 0, the current file offset will not be updated by the read. 3191 --- @param fd integer 3192 --- @param size integer 3193 --- @param offset integer? 3194 --- @return string? data 3195 --- @return string? err 3196 --- @return uv.error_name? err_name 3197 --- @overload fun(fd: integer, size: integer, offset: integer?, callback: fun(err: string?, data: string?)): uv.uv_fs_t 3198 function uv.fs_read(fd, size, offset) end 3199 3200 --- Equivalent to `unlink(2)`. 3201 --- @param path string 3202 --- @return boolean? success 3203 --- @return string? err 3204 --- @return uv.error_name? err_name 3205 --- @overload fun(path: string, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3206 function uv.fs_unlink(path) end 3207 3208 --- Equivalent to `pwritev(2)`. Returns the number of bytes written. 3209 --- 3210 --- If `offset` is nil or omitted, it will default to `-1`, which indicates 'use and update the current file offset.' 3211 --- 3212 --- **Note:** When `offset` is >= 0, the current file offset will not be updated by the write. 3213 --- @param fd integer 3214 --- @param data uv.buffer 3215 --- @param offset integer? 3216 --- @return integer? bytes_written 3217 --- @return string? err 3218 --- @return uv.error_name? err_name 3219 --- @overload fun(fd: integer, data: uv.buffer, offset: integer?, callback: fun(err: string?, bytes: integer?)): uv.uv_fs_t 3220 function uv.fs_write(fd, data, offset) end 3221 3222 --- Equivalent to `mkdir(2)`. 3223 --- @param path string 3224 --- @param mode integer (octal `chmod(1)` mode, e.g. `tonumber('755', 8)`) 3225 --- @return boolean? success 3226 --- @return string? err 3227 --- @return uv.error_name? err_name 3228 --- @overload fun(path: string, mode: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3229 function uv.fs_mkdir(path, mode) end 3230 3231 --- Equivalent to `mkdtemp(3)`. 3232 --- @param template string 3233 --- @return string? path 3234 --- @return string? err 3235 --- @return uv.error_name? err_name 3236 --- @overload fun(template: string, callback: fun(err: string?, path: string?)): uv.uv_fs_t 3237 function uv.fs_mkdtemp(template) end 3238 3239 --- @alias uv.fs_mkstemp.callback 3240 --- | fun(err: string?, fd: integer?, path: string?) 3241 3242 --- Equivalent to `mkstemp(3)`. Returns a temporary file handle and filename. 3243 --- @param template string 3244 --- @return integer? fd 3245 --- @return string path_or_err 3246 --- @return uv.error_name? err_name 3247 --- @overload fun(template: string, callback: uv.fs_mkstemp.callback): uv.uv_fs_t 3248 function uv.fs_mkstemp(template) end 3249 3250 --- Equivalent to `rmdir(2)`. 3251 --- @param path string 3252 --- @return boolean? success 3253 --- @return string? err 3254 --- @return uv.error_name? err_name 3255 --- @overload fun(path: string, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3256 function uv.fs_rmdir(path) end 3257 3258 --- Equivalent to `scandir(3)`, with a slightly different API. Returns a handle that 3259 --- the user can pass to `uv.fs_scandir_next()`. 3260 --- 3261 --- **Note:** This function can be used synchronously or asynchronously. The request 3262 --- userdata is always synchronously returned regardless of whether a callback is 3263 --- provided and the same userdata is passed to the callback if it is provided. 3264 --- @param path string 3265 --- @param callback fun(err: string?, success: uv.uv_fs_t?)? 3266 --- @return uv.uv_fs_t? handle 3267 --- @return string? err 3268 --- @return uv.error_name? err_name 3269 function uv.fs_scandir(path, callback) end 3270 3271 --- Called on a `uv_fs_t` returned by `uv.fs_scandir()` to get the next directory 3272 --- entry data as a `name, type` pair. When there are no more entries, `nil` is 3273 --- returned. 3274 --- 3275 --- **Note:** This function only has a synchronous version. See `uv.fs_opendir` and 3276 --- its related functions for an asynchronous version. 3277 --- @param fs uv.uv_fs_t 3278 --- @return string? name 3279 --- @return string type_or_err 3280 --- @return uv.error_name? err_name 3281 function uv.fs_scandir_next(fs) end 3282 3283 --- @class uv.fs_stat.result 3284 --- @field dev integer 3285 --- @field mode integer 3286 --- @field nlink integer 3287 --- @field uid integer 3288 --- @field gid integer 3289 --- @field rdev integer 3290 --- @field ino integer 3291 --- @field size integer 3292 --- @field blksize integer 3293 --- @field blocks integer 3294 --- @field flags integer 3295 --- @field gen integer 3296 --- @field atime uv.fs_stat.result.time 3297 --- @field mtime uv.fs_stat.result.time 3298 --- @field ctime uv.fs_stat.result.time 3299 --- @field birthtime uv.fs_stat.result.time 3300 --- @field type string 3301 3302 --- @class uv.fs_stat.result.time 3303 --- @field sec integer 3304 --- @field nsec integer 3305 3306 --- @class uv.fs_statfs.result 3307 --- @field type integer 3308 --- @field bsize integer 3309 --- @field blocks integer 3310 --- @field bfree integer 3311 --- @field bavail integer 3312 --- @field files integer 3313 --- @field ffree integer 3314 3315 --- Equivalent to `stat(2)`. 3316 --- @param path string 3317 --- @return uv.fs_stat.result? stat 3318 --- @return string? err 3319 --- @return uv.error_name? err_name 3320 --- @overload fun(path: string, callback: fun(err: string?, stat: uv.fs_stat.result?)): uv.uv_fs_t 3321 function uv.fs_stat(path) end 3322 3323 --- Equivalent to `fstat(2)`. 3324 --- @param fd integer 3325 --- @return uv.fs_stat.result? stat 3326 --- @return string? err 3327 --- @return uv.error_name? err_name 3328 --- @overload fun(fd: integer, callback: fun(err: string?, stat: uv.fs_stat.result?)): uv.uv_fs_t 3329 function uv.fs_fstat(fd) end 3330 3331 --- Equivalent to `lstat(2)`. 3332 --- @param path string 3333 --- @return uv.fs_stat.result? stat 3334 --- @return string? err 3335 --- @return uv.error_name? err_name 3336 --- @overload fun(path: string, callback: fun(err: string?, stat: uv.fs_stat.result?)): uv.uv_fs_t 3337 function uv.fs_lstat(path) end 3338 3339 --- Equivalent to `rename(2)`. 3340 --- @param path string 3341 --- @param new_path string 3342 --- @return boolean? success 3343 --- @return string? err 3344 --- @return uv.error_name? err_name 3345 --- @overload fun(path: string, new_path: string, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3346 function uv.fs_rename(path, new_path) end 3347 3348 --- Equivalent to `fsync(2)`. 3349 --- @param fd integer 3350 --- @return boolean? success 3351 --- @return string? err 3352 --- @return uv.error_name? err_name 3353 --- @overload fun(fd: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3354 function uv.fs_fsync(fd) end 3355 3356 --- Equivalent to `fdatasync(2)`. 3357 --- @param fd integer 3358 --- @return boolean? success 3359 --- @return string? err 3360 --- @return uv.error_name? err_name 3361 --- @overload fun(fd: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3362 function uv.fs_fdatasync(fd) end 3363 3364 --- Equivalent to `ftruncate(2)`. 3365 --- @param fd integer 3366 --- @param offset integer 3367 --- @return boolean? success 3368 --- @return string? err 3369 --- @return uv.error_name? err_name 3370 --- @overload fun(fd: integer, offset: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3371 function uv.fs_ftruncate(fd, offset) end 3372 3373 --- Limited equivalent to `sendfile(2)`. Returns the number of bytes written. 3374 --- @param out_fd integer 3375 --- @param in_fd integer 3376 --- @param in_offset integer 3377 --- @param size integer 3378 --- @return integer? bytes 3379 --- @return string? err 3380 --- @return uv.error_name? err_name 3381 --- @overload fun(out_fd: integer, in_fd: integer, in_offset: integer, size: integer, callback: fun(err: string?, bytes: integer?)): uv.uv_fs_t 3382 function uv.fs_sendfile(out_fd, in_fd, in_offset, size) end 3383 3384 --- Equivalent to `access(2)` on Unix. Windows uses `GetFileAttributesW()`. Access 3385 --- `mode` can be an integer or a string containing `"R"` or `"W"` or `"X"`. 3386 --- Returns `true` or `false` indicating access permission. 3387 --- @param path string 3388 --- @param mode string (a combination of the `'r'`, `'w'` and `'x'` characters denoting the symbolic mode as per `chmod(1)`) 3389 --- @return boolean? permission 3390 --- @return string? err 3391 --- @return uv.error_name? err_name 3392 --- @overload fun(path: string, mode: string, callback: fun(err: string?, permission: boolean?)): uv.uv_fs_t 3393 function uv.fs_access(path, mode) end 3394 3395 --- Equivalent to `chmod(2)`. 3396 --- @param path string 3397 --- @param mode integer (octal `chmod(1)` mode, e.g. `tonumber('644', 8)`) 3398 --- @return boolean? success 3399 --- @return string? err 3400 --- @return uv.error_name? err_name 3401 --- @overload fun(path: string, mode: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3402 function uv.fs_chmod(path, mode) end 3403 3404 --- Equivalent to `fchmod(2)`. 3405 --- @param fd integer 3406 --- @param mode integer 3407 --- @return boolean? success 3408 --- @return string? err 3409 --- @return uv.error_name? err_name 3410 --- @overload fun(fd: integer, mode: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3411 function uv.fs_fchmod(fd, mode) end 3412 3413 --- Equivalent to `utime(2)`. 3414 --- 3415 --- See [Constants][] for supported FS Modification Time constants. 3416 --- 3417 --- Passing `"now"` or `uv.constants.FS_UTIME_NOW` as the atime or mtime sets the timestamp to the 3418 --- current time. 3419 --- 3420 --- Passing `nil`, `"omit"`, or `uv.constants.FS_UTIME_OMIT` as the atime or mtime leaves the timestamp 3421 --- untouched. 3422 --- @param path string 3423 --- @param atime number|string? 3424 --- @param mtime number|string? 3425 --- @return boolean? success 3426 --- @return string? err 3427 --- @return uv.error_name? err_name 3428 --- @overload fun(path: string, atime: number|string?, mtime: number|string?, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3429 function uv.fs_utime(path, atime, mtime) end 3430 3431 --- Equivalent to `futimes(3)`. 3432 --- 3433 --- See [Constants][] for supported FS Modification Time constants. 3434 --- 3435 --- Passing `"now"` or `uv.constants.FS_UTIME_NOW` as the atime or mtime sets the timestamp to the 3436 --- current time. 3437 --- 3438 --- Passing `nil`, `"omit"`, or `uv.constants.FS_UTIME_OMIT` as the atime or mtime leaves the timestamp 3439 --- untouched. 3440 --- @param fd integer 3441 --- @param atime number|string? 3442 --- @param mtime number|string? 3443 --- @return boolean? success 3444 --- @return string? err 3445 --- @return uv.error_name? err_name 3446 --- @overload fun(fd: integer, atime: number|string?, mtime: number|string?, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3447 function uv.fs_futime(fd, atime, mtime) end 3448 3449 --- Equivalent to `lutimes(3)`. 3450 --- 3451 --- See [Constants][] for supported FS Modification Time constants. 3452 --- 3453 --- Passing `"now"` or `uv.constants.FS_UTIME_NOW` as the atime or mtime sets the timestamp to the 3454 --- current time. 3455 --- 3456 --- Passing `nil`, `"omit"`, or `uv.constants.FS_UTIME_OMIT` as the atime or mtime leaves the timestamp 3457 --- untouched. 3458 --- @param path string 3459 --- @param atime number|string? 3460 --- @param mtime number|string? 3461 --- @return boolean? success 3462 --- @return string? err 3463 --- @return uv.error_name? err_name 3464 --- @overload fun(path: string, atime: number|string?, mtime: number|string?, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3465 function uv.fs_lutime(path, atime, mtime) end 3466 3467 --- Equivalent to `link(2)`. 3468 --- @param path string 3469 --- @param new_path string 3470 --- @return boolean? success 3471 --- @return string? err 3472 --- @return uv.error_name? err_name 3473 --- @overload fun(path: string, new_path: string, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3474 function uv.fs_link(path, new_path) end 3475 3476 --- Equivalent to `symlink(2)`. If the `flags` parameter is omitted, then the 3rd parameter will be treated as the `callback`. 3477 --- @param path string 3478 --- @param new_path string 3479 --- @param flags integer|{ dir: boolean?, junction: boolean? }? 3480 --- @return boolean? success 3481 --- @return string? err 3482 --- @return uv.error_name? err_name 3483 --- @overload fun(path: string, new_path: string, flags: integer|{ dir: boolean?, junction: boolean? }?, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3484 function uv.fs_symlink(path, new_path, flags) end 3485 3486 --- Equivalent to `readlink(2)`. 3487 --- @param path string 3488 --- @return string? path 3489 --- @return string? err 3490 --- @return uv.error_name? err_name 3491 --- @overload fun(path: string, callback: fun(err: string?, path: string?)): uv.uv_fs_t 3492 function uv.fs_readlink(path) end 3493 3494 --- Equivalent to `realpath(3)`. 3495 --- @param path string 3496 --- @return string? path 3497 --- @return string? err 3498 --- @return uv.error_name? err_name 3499 --- @overload fun(path: string, callback: fun(err: string?, path: string?)): uv.uv_fs_t 3500 function uv.fs_realpath(path) end 3501 3502 --- Equivalent to `chown(2)`. 3503 --- @param path string 3504 --- @param uid integer 3505 --- @param gid integer 3506 --- @return boolean? success 3507 --- @return string? err 3508 --- @return uv.error_name? err_name 3509 --- @overload fun(path: string, uid: integer, gid: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3510 function uv.fs_chown(path, uid, gid) end 3511 3512 --- Equivalent to `fchown(2)`. 3513 --- @param fd integer 3514 --- @param uid integer 3515 --- @param gid integer 3516 --- @return boolean? success 3517 --- @return string? err 3518 --- @return uv.error_name? err_name 3519 --- @overload fun(fd: integer, uid: integer, gid: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3520 function uv.fs_fchown(fd, uid, gid) end 3521 3522 --- Equivalent to `lchown(2)`. 3523 --- @param fd integer 3524 --- @param uid integer 3525 --- @param gid integer 3526 --- @return boolean? success 3527 --- @return string? err 3528 --- @return uv.error_name? err_name 3529 --- @overload fun(fd: integer, uid: integer, gid: integer, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3530 function uv.fs_lchown(fd, uid, gid) end 3531 3532 --- @class uv.fs_copyfile.flags 3533 --- @field excl boolean? 3534 --- @field ficlone boolean? 3535 --- @field ficlone_force boolean? 3536 3537 --- Copies a file from path to new_path. If the `flags` parameter is omitted, then the 3rd parameter will be treated as the `callback`. 3538 --- @param path string 3539 --- @param new_path string 3540 --- @param flags integer|uv.fs_copyfile.flags? 3541 --- @return boolean? success 3542 --- @return string? err 3543 --- @return uv.error_name? err_name 3544 --- @overload fun(path: string, new_path: string, flags: integer|uv.fs_copyfile.flags?, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3545 function uv.fs_copyfile(path, new_path, flags) end 3546 3547 --- Opens path as a directory stream. Returns a handle that the user can pass to 3548 --- `uv.fs_readdir()`. The `entries` parameter defines the maximum number of entries 3549 --- that should be returned by each call to `uv.fs_readdir()`. 3550 --- @param path string 3551 --- @param callback nil (async if provided, sync if `nil`) 3552 --- @param entries integer? 3553 --- @return uv.luv_dir_t? dir 3554 --- @return string? err 3555 --- @return uv.error_name? err_name 3556 --- @overload fun(path: string, callback: fun(err: string?, dir: uv.luv_dir_t?), entries: integer?): uv.uv_fs_t 3557 function uv.fs_opendir(path, callback, entries) end 3558 3559 --- Iterates over the directory stream `luv_dir_t` returned by a successful 3560 --- `uv.fs_opendir()` call. A table of data tables is returned where the number 3561 --- of entries `n` is equal to or less than the `entries` parameter used in 3562 --- the associated `uv.fs_opendir()` call. 3563 --- @param dir uv.luv_dir_t 3564 --- @return table<integer, { name: string, type: string }>? entries 3565 --- @return string? err 3566 --- @return uv.error_name? err_name 3567 --- @overload fun(dir: uv.luv_dir_t, callback: fun(err: string?, entries: table<integer, { name: string, type: string }>?)): uv.uv_fs_t 3568 function uv.fs_readdir(dir) end 3569 3570 --- @class uv.luv_dir_t : userdata 3571 local luv_dir_t = {} 3572 3573 --- Iterates over the directory stream `luv_dir_t` returned by a successful 3574 --- `uv.fs_opendir()` call. A table of data tables is returned where the number 3575 --- of entries `n` is equal to or less than the `entries` parameter used in 3576 --- the associated `uv.fs_opendir()` call. 3577 --- @return table<integer, { name: string, type: string }>? entries 3578 --- @return string? err 3579 --- @return uv.error_name? err_name 3580 --- @overload fun(dir: uv.luv_dir_t, callback: fun(err: string?, entries: table<integer, { name: string, type: string }>?)): uv.uv_fs_t 3581 function luv_dir_t:readdir() end 3582 3583 --- Closes a directory stream returned by a successful `uv.fs_opendir()` call. 3584 --- @param dir uv.luv_dir_t 3585 --- @return boolean? success 3586 --- @return string? err 3587 --- @return uv.error_name? err_name 3588 --- @overload fun(dir: uv.luv_dir_t, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3589 function uv.fs_closedir(dir) end 3590 3591 --- Closes a directory stream returned by a successful `uv.fs_opendir()` call. 3592 --- @return boolean? success 3593 --- @return string? err 3594 --- @return uv.error_name? err_name 3595 --- @overload fun(dir: uv.luv_dir_t, callback: fun(err: string?, success: boolean?)): uv.uv_fs_t 3596 function luv_dir_t:closedir() end 3597 3598 --- Equivalent to `statfs(2)`. 3599 --- @param path string 3600 --- @return uv.fs_statfs.result? stat 3601 --- @return string? err 3602 --- @return uv.error_name? err_name 3603 --- @overload fun(path: string, callback: fun(err: string?, stat: uv.fs_statfs.result?)): uv.uv_fs_t 3604 function uv.fs_statfs(path) end 3605 3606 3607 --- # Thread pool work scheduling 3608 --- 3609 --- Libuv provides a threadpool which can be used to run user code and get notified 3610 --- in the loop thread. This threadpool is internally used to run all file system 3611 --- operations, as well as `getaddrinfo` and `getnameinfo` requests. 3612 --- 3613 --- ```lua 3614 --- local function work_callback(a, b) 3615 --- return a + b 3616 --- end 3617 --- 3618 --- local function after_work_callback(c) 3619 --- print("The result is: " .. c) 3620 --- end 3621 --- 3622 --- local work = uv.new_work(work_callback, after_work_callback) 3623 --- 3624 --- work:queue(1, 2) 3625 --- 3626 --- -- output: "The result is: 3" 3627 --- ``` 3628 3629 --- Creates and initializes a new `luv_work_ctx_t` (not `uv_work_t`). 3630 --- `work_callback` is a Lua function or a string containing Lua code or bytecode dumped from a function. 3631 --- Returns the Lua userdata wrapping it. 3632 --- @param work_callback string|fun(...: uv.threadargs) 3633 --- @param after_work_callback fun(...: uv.threadargs) 3634 --- @return uv.luv_work_ctx_t 3635 function uv.new_work(work_callback, after_work_callback) end 3636 3637 --- Queues a work request which will run `work_callback` in a new Lua state in a 3638 --- thread from the threadpool with any additional arguments from `...`. Values 3639 --- returned from `work_callback` are passed to `after_work_callback`, which is 3640 --- called in the main loop thread. 3641 --- @param work_ctx uv.luv_work_ctx_t 3642 --- @param ... uv.threadargs 3643 --- @return boolean? success 3644 --- @return string? err 3645 --- @return uv.error_name? err_name 3646 function uv.queue_work(work_ctx, ...) end 3647 3648 --- @class uv.luv_work_ctx_t : userdata 3649 local luv_work_ctx_t = {} 3650 3651 --- Queues a work request which will run `work_callback` in a new Lua state in a 3652 --- thread from the threadpool with any additional arguments from `...`. Values 3653 --- returned from `work_callback` are passed to `after_work_callback`, which is 3654 --- called in the main loop thread. 3655 --- @param ... uv.threadargs 3656 --- @return boolean? success 3657 --- @return string? err 3658 --- @return uv.error_name? err_name 3659 function luv_work_ctx_t:queue(...) end 3660 3661 3662 --- # DNS utility functions 3663 3664 --- @class uv.getaddrinfo.hints 3665 --- @field family string|integer? 3666 --- @field socktype string|integer? 3667 --- @field protocol string|integer? 3668 --- @field addrconfig boolean? 3669 --- @field v4mapped boolean? 3670 --- @field all boolean? 3671 --- @field numerichost boolean? 3672 --- @field passive boolean? 3673 --- @field numericserv boolean? 3674 --- @field canonname boolean? 3675 3676 --- Equivalent to `getaddrinfo(3)`. Either `node` or `service` may be `nil` but not 3677 --- both. 3678 --- 3679 --- See [Constants][] for supported address `family` input and output values. 3680 --- 3681 --- See [Constants][] for supported `socktype` input and output values. 3682 --- 3683 --- When `protocol` is set to 0 or nil, it will be automatically chosen based on the 3684 --- socket's domain and type. When `protocol` is specified as a string, it will be 3685 --- looked up using the `getprotobyname(3)` function. Examples: `"ip"`, `"icmp"`, 3686 --- `"tcp"`, `"udp"`, etc. 3687 --- @param host string? 3688 --- @param service string? 3689 --- @param hints uv.getaddrinfo.hints? 3690 --- @return table<integer, uv.address>? addresses 3691 --- @return string? err 3692 --- @return uv.error_name? err_name 3693 --- @overload fun(host: string?, service: string?, hints: uv.getaddrinfo.hints?, callback: fun(err: string?, addresses: table<integer, uv.address>?)): uv.uv_getaddrinfo_t?, string?, uv.error_name? 3694 function uv.getaddrinfo(host, service, hints) end 3695 3696 --- @class uv.getnameinfo.address 3697 --- @field ip string? 3698 --- @field port integer? 3699 --- @field family string|integer? 3700 3701 --- @alias uv.getnameinfo.callback 3702 --- | fun(err: string?, host: string?, service: string?) 3703 3704 --- Equivalent to `getnameinfo(3)`. 3705 --- 3706 --- See [Constants][] for supported address `family` input values. 3707 --- @param address uv.getnameinfo.address 3708 --- @return string? host 3709 --- @return string service_or_err 3710 --- @return uv.error_name? err_name 3711 --- @overload fun(address: uv.getnameinfo.address, callback: uv.getnameinfo.callback): uv.uv_getnameinfo_t?, string?, uv.error_name? 3712 function uv.getnameinfo(address) end 3713 3714 3715 --- # Threading and synchronization utilities 3716 --- 3717 --- Libuv provides cross-platform implementations for multiple threading and 3718 --- synchronization primitives. The API largely follows the pthreads API. 3719 3720 --- Creates and initializes a `luv_thread_t` (not `uv_thread_t`). Returns the Lua 3721 --- userdata wrapping it and asynchronously executes `entry`, which can be either 3722 --- a Lua function or a string containing Lua code or bytecode dumped from a function. Additional arguments `...` 3723 --- are passed to the `entry` function and an optional `options` table may be 3724 --- provided. Currently accepted `option` fields are `stack_size`. 3725 --- **Note**: 3726 --- unsafe, please make sure the thread end of life before Lua state close. 3727 --- @param options { stack_size: integer? }? 3728 --- @param entry function|string 3729 --- @param ... uv.threadargs passed to `entry` 3730 --- @return uv.luv_thread_t? thread 3731 --- @return string? err 3732 --- @return uv.error_name? err_name 3733 function uv.new_thread(options, entry, ...) end 3734 3735 --- Returns a boolean indicating whether two threads are the same. This function is 3736 --- equivalent to the `__eq` metamethod. 3737 --- @param thread uv.luv_thread_t 3738 --- @param other_thread uv.luv_thread_t 3739 --- @return boolean 3740 function uv.thread_equal(thread, other_thread) end 3741 3742 --- @class uv.luv_thread_t : userdata 3743 local luv_thread_t = {} 3744 3745 --- Returns a boolean indicating whether two threads are the same. This function is 3746 --- equivalent to the `__eq` metamethod. 3747 --- @param other_thread uv.luv_thread_t 3748 --- @return boolean 3749 function luv_thread_t:equal(other_thread) end 3750 3751 --- Sets the specified thread's affinity setting. 3752 --- 3753 --- `affinity` must be a table where each of the keys are a CPU number and the 3754 --- values are booleans that represent whether the `thread` should be eligible to 3755 --- run on that CPU. If the length of the `affinity` table is not greater than or 3756 --- equal to `uv.cpumask_size()`, any CPU numbers missing from the table will have 3757 --- their affinity set to `false`. If setting the affinity of more than 3758 --- `uv.cpumask_size()` CPUs is desired, `affinity` must be an array-like table 3759 --- with no gaps, since `#affinity` will be used as the `cpumask_size` if it is 3760 --- greater than `uv.cpumask_size()`. 3761 --- 3762 --- If `get_old_affinity` is `true`, the previous affinity settings for the `thread` 3763 --- will be returned. Otherwise, `true` is returned after a successful call. 3764 --- 3765 --- **Note:** Thread affinity setting is not atomic on Windows. Unsupported on macOS. 3766 --- @param thread uv.luv_thread_t 3767 --- @param affinity table<integer, boolean> 3768 --- @param get_old_affinity boolean? 3769 --- @return table<integer, boolean>? affinity 3770 --- @return string? err 3771 --- @return uv.error_name? err_name 3772 function uv.thread_setaffinity(thread, affinity, get_old_affinity) end 3773 3774 --- Sets the specified thread's affinity setting. 3775 --- 3776 --- `affinity` must be a table where each of the keys are a CPU number and the 3777 --- values are booleans that represent whether the `thread` should be eligible to 3778 --- run on that CPU. If the length of the `affinity` table is not greater than or 3779 --- equal to `uv.cpumask_size()`, any CPU numbers missing from the table will have 3780 --- their affinity set to `false`. If setting the affinity of more than 3781 --- `uv.cpumask_size()` CPUs is desired, `affinity` must be an array-like table 3782 --- with no gaps, since `#affinity` will be used as the `cpumask_size` if it is 3783 --- greater than `uv.cpumask_size()`. 3784 --- 3785 --- If `get_old_affinity` is `true`, the previous affinity settings for the `thread` 3786 --- will be returned. Otherwise, `true` is returned after a successful call. 3787 --- 3788 --- **Note:** Thread affinity setting is not atomic on Windows. Unsupported on macOS. 3789 --- @param affinity table<integer, boolean> 3790 --- @param get_old_affinity boolean? 3791 --- @return table<integer, boolean>? affinity 3792 --- @return string? err 3793 --- @return uv.error_name? err_name 3794 function luv_thread_t:setaffinity(affinity, get_old_affinity) end 3795 3796 --- Gets the specified thread's affinity setting. 3797 --- 3798 --- If `mask_size` is provided, it must be greater than or equal to 3799 --- `uv.cpumask_size()`. If the `mask_size` parameter is omitted, then the return 3800 --- of `uv.cpumask_size()` will be used. Returns an array-like table where each of 3801 --- the keys correspond to a CPU number and the values are booleans that represent 3802 --- whether the `thread` is eligible to run on that CPU. 3803 --- 3804 --- **Note:** Thread affinity getting is not atomic on Windows. Unsupported on macOS. 3805 --- @param thread uv.luv_thread_t 3806 --- @param mask_size integer? 3807 --- @return table<integer, boolean>? affinity 3808 --- @return string? err 3809 --- @return uv.error_name? err_name 3810 function uv.thread_getaffinity(thread, mask_size) end 3811 3812 --- Gets the specified thread's affinity setting. 3813 --- 3814 --- If `mask_size` is provided, it must be greater than or equal to 3815 --- `uv.cpumask_size()`. If the `mask_size` parameter is omitted, then the return 3816 --- of `uv.cpumask_size()` will be used. Returns an array-like table where each of 3817 --- the keys correspond to a CPU number and the values are booleans that represent 3818 --- whether the `thread` is eligible to run on that CPU. 3819 --- 3820 --- **Note:** Thread affinity getting is not atomic on Windows. Unsupported on macOS. 3821 --- @param mask_size integer? 3822 --- @return table<integer, boolean>? affinity 3823 --- @return string? err 3824 --- @return uv.error_name? err_name 3825 function luv_thread_t:getaffinity(mask_size) end 3826 3827 --- Gets the CPU number on which the calling thread is running. 3828 --- 3829 --- **Note:** The first CPU will be returned as the number 1, not 0. This allows for 3830 --- the number to correspond with the table keys used in `uv.thread_getaffinity` and 3831 --- `uv.thread_setaffinity`. 3832 --- @return integer? cpu 3833 --- @return string? err 3834 --- @return uv.error_name? err_name 3835 function uv.thread_getcpu() end 3836 3837 --- Sets the specified thread's scheduling priority setting. It requires elevated 3838 --- privilege to set specific priorities on some platforms. 3839 --- 3840 --- The priority can be set to the following constants. 3841 --- 3842 --- - uv.constants.THREAD_PRIORITY_HIGHEST 3843 --- - uv.constants.THREAD_PRIORITY_ABOVE_NORMAL 3844 --- - uv.constants.THREAD_PRIORITY_NORMAL 3845 --- - uv.constants.THREAD_PRIORITY_BELOW_NORMAL 3846 --- - uv.constants.THREAD_PRIORITY_LOWEST 3847 --- @param thread uv.luv_thread_t 3848 --- @param priority integer 3849 --- @return boolean? success 3850 --- @return string? err 3851 --- @return uv.error_name? err_name 3852 function uv.thread_setpriority(thread, priority) end 3853 3854 --- Sets the specified thread's scheduling priority setting. It requires elevated 3855 --- privilege to set specific priorities on some platforms. 3856 --- 3857 --- The priority can be set to the following constants. 3858 --- 3859 --- - uv.constants.THREAD_PRIORITY_HIGHEST 3860 --- - uv.constants.THREAD_PRIORITY_ABOVE_NORMAL 3861 --- - uv.constants.THREAD_PRIORITY_NORMAL 3862 --- - uv.constants.THREAD_PRIORITY_BELOW_NORMAL 3863 --- - uv.constants.THREAD_PRIORITY_LOWEST 3864 --- @param priority integer 3865 --- @return boolean? success 3866 --- @return string? err 3867 --- @return uv.error_name? err_name 3868 function luv_thread_t:setpriority(priority) end 3869 3870 --- Gets the thread's priority setting. 3871 --- 3872 --- Retrieves the scheduling priority of the specified thread. The returned priority 3873 --- value is platform dependent. 3874 --- 3875 --- For Linux, when schedule policy is SCHED_OTHER (default), priority is 0. 3876 --- @param thread uv.luv_thread_t 3877 --- @return integer? priority 3878 --- @return string? err 3879 --- @return uv.error_name? err_name 3880 function uv.thread_getpriority(thread) end 3881 3882 --- Gets the thread's priority setting. 3883 --- 3884 --- Retrieves the scheduling priority of the specified thread. The returned priority 3885 --- value is platform dependent. 3886 --- 3887 --- For Linux, when schedule policy is SCHED_OTHER (default), priority is 0. 3888 --- @return integer? priority 3889 --- @return string? err 3890 --- @return uv.error_name? err_name 3891 function luv_thread_t:getpriority() end 3892 3893 --- Returns the handle for the thread in which this is called. 3894 --- @return uv.luv_thread_t 3895 function uv.thread_self() end 3896 3897 --- Waits for the `thread` to finish executing its entry function. 3898 --- @param thread uv.luv_thread_t 3899 --- @return boolean? success 3900 --- @return string? err 3901 --- @return uv.error_name? err_name 3902 function uv.thread_join(thread) end 3903 3904 --- Waits for the `thread` to finish executing its entry function. 3905 --- @return boolean? success 3906 --- @return string? err 3907 --- @return uv.error_name? err_name 3908 function luv_thread_t:join() end 3909 3910 --- Detaches a thread. Detached threads automatically release their resources upon 3911 --- termination, eliminating the need for the application to call `uv.thread_join`. 3912 --- @param thread uv.luv_thread_t 3913 --- @return boolean? success 3914 --- @return string? err 3915 --- @return uv.error_name? err_name 3916 function uv.thread_detach(thread) end 3917 3918 --- Detaches a thread. Detached threads automatically release their resources upon 3919 --- termination, eliminating the need for the application to call `uv.thread_join`. 3920 --- @return boolean? success 3921 --- @return string? err 3922 --- @return uv.error_name? err_name 3923 function luv_thread_t:detach() end 3924 3925 --- Sets the name of the current thread. Different platforms define different limits 3926 --- on the max number of characters a thread name can be: Linux, IBM i (16), macOS 3927 --- (64), Windows (32767), and NetBSD (32), etc. The name will be truncated 3928 --- if `name` is larger than the limit of the platform. 3929 --- @param name string 3930 --- @return 0? success 3931 --- @return string? err 3932 --- @return uv.error_name? err_name 3933 function uv.thread_setname(name) end 3934 3935 --- Gets the name of the thread specified by `thread`. 3936 --- @param thread uv.luv_thread_t 3937 --- @return string? name 3938 --- @return string? err 3939 --- @return uv.error_name? err_name 3940 function uv.thread_getname(thread) end 3941 3942 --- Gets the name of the thread specified by `thread`. 3943 --- @return string? name 3944 --- @return string? err 3945 --- @return uv.error_name? err_name 3946 function luv_thread_t:getname() end 3947 3948 --- Pauses the thread in which this is called for a number of milliseconds. 3949 --- @param msec integer 3950 function uv.sleep(msec) end 3951 3952 --- Creates a new semaphore with the specified initial value. A semaphore is safe to 3953 --- share across threads. It represents an unsigned integer value that can incremented 3954 --- and decremented atomically but any attempt to make it negative will "wait" until 3955 --- the value can be decremented by another thread incrementing it. 3956 --- 3957 --- The initial value must be a non-negative integer. 3958 --- **Note**: 3959 --- A semaphore must be shared between threads, any `uv.sem_wait()` on a single thread that blocks will deadlock. 3960 --- @param value integer? 3961 --- @return uv.luv_sem_t? sem 3962 --- @return string? err 3963 --- @return uv.error_name? err_name 3964 function uv.new_sem(value) end 3965 3966 --- Increments (unlocks) a semaphore, if the semaphore's value consequently becomes 3967 --- greater than zero then another thread blocked in a sem_wait call will be woken 3968 --- and proceed to decrement the semaphore. 3969 --- @param sem uv.luv_sem_t 3970 function uv.sem_post(sem) end 3971 3972 --- @class uv.luv_sem_t : userdata 3973 local luv_sem_t = {} 3974 3975 --- Increments (unlocks) a semaphore, if the semaphore's value consequently becomes 3976 --- greater than zero then another thread blocked in a sem_wait call will be woken 3977 --- and proceed to decrement the semaphore. 3978 function luv_sem_t:post() end 3979 3980 --- Decrements (locks) a semaphore, if the semaphore's value is greater than zero 3981 --- then the value is decremented and the call returns immediately. If the semaphore's 3982 --- value is zero then the call blocks until the semaphore's value rises above zero or 3983 --- the call is interrupted by a signal. 3984 --- @param sem uv.luv_sem_t 3985 function uv.sem_wait(sem) end 3986 3987 --- Decrements (locks) a semaphore, if the semaphore's value is greater than zero 3988 --- then the value is decremented and the call returns immediately. If the semaphore's 3989 --- value is zero then the call blocks until the semaphore's value rises above zero or 3990 --- the call is interrupted by a signal. 3991 function luv_sem_t:wait() end 3992 3993 --- The same as `uv.sem_wait()` but returns immediately if the semaphore is not available. 3994 --- 3995 --- If the semaphore's value was decremented then `true` is returned, otherwise the semaphore 3996 --- has a value of zero and `false` is returned. 3997 --- @param sem uv.luv_sem_t 3998 --- @return boolean 3999 function uv.sem_trywait(sem) end 4000 4001 --- The same as `uv.sem_wait()` but returns immediately if the semaphore is not available. 4002 --- 4003 --- If the semaphore's value was decremented then `true` is returned, otherwise the semaphore 4004 --- has a value of zero and `false` is returned. 4005 --- @return boolean 4006 function luv_sem_t:trywait() end 4007 4008 4009 --- # Miscellaneous utilities 4010 4011 --- Returns the executable path. 4012 --- @return string? path 4013 --- @return string? err 4014 --- @return uv.error_name? err_name 4015 function uv.exepath() end 4016 4017 --- Returns the current working directory. 4018 --- @return string? path 4019 --- @return string? err 4020 --- @return uv.error_name? err_name 4021 function uv.cwd() end 4022 4023 --- Sets the current working directory with the string `cwd`. 4024 --- @param cwd string 4025 --- @return 0? success 4026 --- @return string? err 4027 --- @return uv.error_name? err_name 4028 function uv.chdir(cwd) end 4029 4030 --- Returns the title of the current process. 4031 --- @return string? title 4032 --- @return string? err 4033 --- @return uv.error_name? err_name 4034 function uv.get_process_title() end 4035 4036 --- Sets the title of the current process with the string `title`. 4037 --- @param title string 4038 --- @return 0? success 4039 --- @return string? err 4040 --- @return uv.error_name? err_name 4041 function uv.set_process_title(title) end 4042 4043 --- Returns the current total system memory in bytes. 4044 --- @return number 4045 function uv.get_total_memory() end 4046 4047 --- Returns the current free system memory in bytes. 4048 --- @return number 4049 function uv.get_free_memory() end 4050 4051 --- Gets the amount of memory available to the process in bytes based on limits 4052 --- imposed by the OS. If there is no such constraint, or the constraint is unknown, 4053 --- 0 is returned. Note that it is not unusual for this value to be less than or 4054 --- greater than the total system memory. 4055 --- @return number 4056 function uv.get_constrained_memory() end 4057 4058 --- Gets the amount of free memory that is still available to the process (in 4059 --- bytes). This differs from `uv.get_free_memory()` in that it takes into account 4060 --- any limits imposed by the OS. If there is no such constraint, or the constraint 4061 --- is unknown, the amount returned will be identical to `uv.get_free_memory()`. 4062 --- @return number 4063 function uv.get_available_memory() end 4064 4065 --- Returns the resident set size (RSS) for the current process. 4066 --- @return integer? rss 4067 --- @return string? err 4068 --- @return uv.error_name? err_name 4069 function uv.resident_set_memory() end 4070 4071 --- @class uv.getrusage.result 4072 --- 4073 --- (user CPU time used) 4074 --- @field utime uv.getrusage.result.time 4075 --- 4076 --- (system CPU time used) 4077 --- @field stime uv.getrusage.result.time 4078 --- 4079 --- (maximum resident set size) 4080 --- @field maxrss integer 4081 --- 4082 --- (integral shared memory size) 4083 --- @field ixrss integer 4084 --- 4085 --- (integral unshared data size) 4086 --- @field idrss integer 4087 --- 4088 --- (integral unshared stack size) 4089 --- @field isrss integer 4090 --- 4091 --- (page reclaims (soft page faults)) 4092 --- @field minflt integer 4093 --- 4094 --- (page faults (hard page faults)) 4095 --- @field majflt integer 4096 --- 4097 --- (swaps) 4098 --- @field nswap integer 4099 --- 4100 --- (block input operations) 4101 --- @field inblock integer 4102 --- 4103 --- (block output operations) 4104 --- @field oublock integer 4105 --- 4106 --- (IPC messages sent) 4107 --- @field msgsnd integer 4108 --- 4109 --- (IPC messages received) 4110 --- @field msgrcv integer 4111 --- 4112 --- (signals received) 4113 --- @field nsignals integer 4114 --- 4115 --- (voluntary context switches) 4116 --- @field nvcsw integer 4117 --- 4118 --- (involuntary context switches) 4119 --- @field nivcsw integer 4120 4121 --- @class uv.getrusage.result.time 4122 --- @field sec integer 4123 --- @field usec integer 4124 4125 --- Returns the resource usage. 4126 --- @return uv.getrusage.result? rusage 4127 --- @return string? err 4128 --- @return uv.error_name? err_name 4129 function uv.getrusage() end 4130 4131 --- Gets the resource usage measures for the calling thread. 4132 --- 4133 --- **Note** Not supported on all platforms. May return `ENOTSUP`. 4134 --- On macOS and Windows not all fields are set (the unsupported fields are filled 4135 --- with zeroes). 4136 --- @return uv.getrusage.result? rusage 4137 --- @return string? err 4138 --- @return uv.error_name? err_name 4139 function uv.getrusage_thread() end 4140 4141 --- Returns an estimate of the default amount of parallelism a program should use. Always returns a non-zero value. 4142 --- 4143 --- On Linux, inspects the calling thread’s CPU affinity mask to determine if it has been pinned to specific CPUs. 4144 --- 4145 --- On Windows, the available parallelism may be underreported on systems with more than 64 logical CPUs. 4146 --- 4147 --- On other platforms, reports the number of CPUs that the operating system considers to be online. 4148 --- @return integer 4149 function uv.available_parallelism() end 4150 4151 --- @class uv.cpu_info.cpu_info 4152 --- @field model string 4153 --- @field speed integer 4154 --- @field times uv.cpu_info.cpu_info.times 4155 4156 --- @class uv.cpu_info.cpu_info.times 4157 --- @field user integer 4158 --- @field nice integer 4159 --- @field sys integer 4160 --- @field idle integer 4161 --- @field irq integer 4162 4163 --- Returns information about the CPU(s) on the system as a table of tables for each 4164 --- CPU found. 4165 --- @return table<integer, uv.cpu_info.cpu_info>? cpu_info 4166 --- @return string? err 4167 --- @return uv.error_name? err_name 4168 function uv.cpu_info() end 4169 4170 --- Returns the maximum size of the mask used for process/thread affinities, or 4171 --- `ENOTSUP` if affinities are not supported on the current platform. 4172 --- @return integer? size 4173 --- @return string? err 4174 --- @return uv.error_name? err_name 4175 function uv.cpumask_size() end 4176 4177 --- @deprecated Please use `uv.os_getpid()` instead. 4178 --- @return integer 4179 function uv.getpid() end 4180 4181 --- Returns the user ID of the process. 4182 --- **Note**: 4183 --- This is not a libuv function and is not supported on Windows. 4184 --- @return integer 4185 function uv.getuid() end 4186 4187 --- Returns the group ID of the process. 4188 --- **Note**: 4189 --- This is not a libuv function and is not supported on Windows. 4190 --- @return integer 4191 function uv.getgid() end 4192 4193 --- Sets the user ID of the process with the integer `id`. 4194 --- **Note**: 4195 --- This is not a libuv function and is not supported on Windows. 4196 --- @param id integer 4197 function uv.setuid(id) end 4198 4199 --- Sets the group ID of the process with the integer `id`. 4200 --- **Note**: 4201 --- This is not a libuv function and is not supported on Windows. 4202 --- @param id integer 4203 function uv.setgid(id) end 4204 4205 --- Returns a current high-resolution time in nanoseconds as a number. This is 4206 --- relative to an arbitrary time in the past. It is not related to the time of day 4207 --- and therefore not subject to clock drift. The primary use is for measuring 4208 --- time between intervals. 4209 --- @return integer 4210 function uv.hrtime() end 4211 4212 --- Obtain the current system time from a high-resolution real-time or monotonic 4213 --- clock source. `clock_id` can be the string `"monotonic"` or `"realtime"`. 4214 --- 4215 --- The real-time clock counts from the UNIX epoch (1970-01-01) and is subject 4216 --- to time adjustments; it can jump back in time. 4217 --- 4218 --- The monotonic clock counts from an arbitrary point in the past and never 4219 --- jumps back in time. 4220 --- @param clock_id string 4221 --- @return { sec: integer, nsec: integer }? time 4222 --- @return string? err 4223 --- @return uv.error_name? err_name 4224 function uv.clock_gettime(clock_id) end 4225 4226 --- Returns the current system uptime in seconds. 4227 --- @return number? uptime 4228 --- @return string? err 4229 --- @return uv.error_name? err_name 4230 function uv.uptime() end 4231 4232 --- Prints all handles associated with the main loop to stderr. The format is 4233 --- `[flags] handle-type handle-address`. Flags are `R` for referenced, `A` for 4234 --- active and `I` for internal. 4235 --- **Note**: 4236 --- This is not available on Windows. 4237 --- **Warning**: 4238 --- This function is meant for ad hoc debugging, there are no API/ABI 4239 --- stability guarantees. 4240 function uv.print_all_handles() end 4241 4242 --- The same as `uv.print_all_handles()` except only active handles are printed. 4243 --- **Note**: 4244 --- This is not available on Windows. 4245 --- **Warning**: 4246 --- This function is meant for ad hoc debugging, there are no API/ABI 4247 --- stability guarantees. 4248 function uv.print_active_handles() end 4249 4250 --- Used to detect what type of stream should be used with a given file 4251 --- descriptor `fd`. Usually this will be used during initialization to guess the 4252 --- type of the stdio streams. 4253 --- @param fd integer 4254 --- @return string 4255 function uv.guess_handle(fd) end 4256 4257 --- Cross-platform implementation of `gettimeofday(2)`. Returns the seconds and 4258 --- microseconds of a unix time as a pair. 4259 --- @return integer? seconds 4260 --- @return integer|string microseconds_or_err 4261 --- @return uv.error_name? err_name 4262 function uv.gettimeofday() end 4263 4264 --- @class uv.interface_addresses.addresses 4265 --- @field ip string 4266 --- @field family string 4267 --- @field netmask string 4268 --- @field internal boolean 4269 --- @field mac string 4270 4271 --- Returns address information about the network interfaces on the system in a 4272 --- table. Each table key is the name of the interface while each associated value 4273 --- is an array of address information where fields are `ip`, `family`, `netmask`, 4274 --- `internal`, and `mac`. 4275 --- 4276 --- See [Constants][] for supported address `family` output values. 4277 --- @return table<string, uv.interface_addresses.addresses> addresses 4278 function uv.interface_addresses() end 4279 4280 --- IPv6-capable implementation of `if_indextoname(3)`. 4281 --- @param ifindex integer 4282 --- @return string? name 4283 --- @return string? err 4284 --- @return uv.error_name? err_name 4285 function uv.if_indextoname(ifindex) end 4286 4287 --- Retrieves a network interface identifier suitable for use in an IPv6 scoped 4288 --- address. On Windows, returns the numeric `ifindex` as a string. On all other 4289 --- platforms, `uv.if_indextoname()` is used. 4290 --- @param ifindex integer 4291 --- @return string? iid 4292 --- @return string? err 4293 --- @return uv.error_name? err_name 4294 function uv.if_indextoiid(ifindex) end 4295 4296 --- Returns the load average as a triad. Not supported on Windows. 4297 --- @return number, number, number 4298 function uv.loadavg() end 4299 4300 --- @class uv.os_uname.info 4301 --- @field sysname string 4302 --- @field release string 4303 --- @field version string 4304 --- @field machine string 4305 4306 --- Returns system information. 4307 --- @return uv.os_uname.info info 4308 function uv.os_uname() end 4309 4310 --- Returns the hostname. 4311 --- @return string 4312 function uv.os_gethostname() end 4313 4314 --- Returns the environment variable specified by `name` as string. The internal 4315 --- buffer size can be set by defining `size`. If omitted, `LUAL_BUFFERSIZE` is 4316 --- used. If the environment variable exceeds the storage available in the internal 4317 --- buffer, `ENOBUFS` is returned. If no matching environment variable exists, 4318 --- `ENOENT` is returned. 4319 --- **Warning**: 4320 --- This function is not thread-safe. 4321 --- @param name string 4322 --- @param size integer? 4323 --- @return string? value 4324 --- @return string? err 4325 --- @return uv.error_name? err_name 4326 function uv.os_getenv(name, size) end 4327 4328 --- Sets the environmental variable specified by `name` with the string `value`. 4329 --- **Warning**: 4330 --- This function is not thread-safe. 4331 --- @param name string 4332 --- @param value string 4333 --- @return boolean? success 4334 --- @return string? err 4335 --- @return uv.error_name? err_name 4336 function uv.os_setenv(name, value) end 4337 4338 --- Unsets the environmental variable specified by `name`. 4339 --- **Warning**: 4340 --- This function is not thread-safe. 4341 --- @param name string 4342 --- @return boolean? success 4343 --- @return string? err 4344 --- @return uv.error_name? err_name 4345 function uv.os_unsetenv(name) end 4346 4347 --- Returns all environmental variables as a dynamic table of names associated with 4348 --- their corresponding values. 4349 --- **Warning**: 4350 --- This function is not thread-safe. 4351 --- @return table 4352 function uv.os_environ() end 4353 4354 --- Returns the home directory. 4355 --- **Warning**: 4356 --- This function is not thread-safe. 4357 --- @return string? path 4358 --- @return string? err 4359 --- @return uv.error_name? err_name 4360 function uv.os_homedir() end 4361 4362 --- Returns a temporary directory. 4363 --- **Warning**: 4364 --- This function is not thread-safe. 4365 --- @return string? path 4366 --- @return string? err 4367 --- @return uv.error_name? err_name 4368 function uv.os_tmpdir() end 4369 4370 --- @class uv.os_get_passwd.passwd 4371 --- @field username string 4372 --- 4373 --- (nil on Windows) 4374 --- @field uid integer? 4375 --- 4376 --- (nil on Windows) 4377 --- @field gid integer? 4378 --- 4379 --- (nil on Windows) 4380 --- @field shell string? 4381 --- @field homedir string 4382 4383 --- Gets a subset of the password file entry for the current effective uid (not the 4384 --- real uid). On Windows, `uid`, `gid`, and `shell` are set to `nil`. 4385 --- @return uv.os_get_passwd.passwd? passwd 4386 --- @return string? err 4387 --- @return uv.error_name? err_name 4388 function uv.os_get_passwd() end 4389 4390 --- Returns the current process ID. 4391 --- @return number 4392 function uv.os_getpid() end 4393 4394 --- Returns the parent process ID. 4395 --- @return number 4396 function uv.os_getppid() end 4397 4398 --- Returns the scheduling priority of the process specified by `pid`. 4399 --- @param pid integer 4400 --- @return integer? priority 4401 --- @return string? err 4402 --- @return uv.error_name? err_name 4403 function uv.os_getpriority(pid) end 4404 4405 --- Sets the scheduling priority of the process specified by `pid`. The `priority` 4406 --- range is between -20 (high priority) and 19 (low priority). 4407 --- @param pid integer 4408 --- @param priority integer 4409 --- @return boolean? success 4410 --- @return string? err 4411 --- @return uv.error_name? err_name 4412 function uv.os_setpriority(pid, priority) end 4413 4414 --- Fills a string of length `len` with cryptographically strong random bytes 4415 --- acquired from the system CSPRNG. `flags` is reserved for future extension 4416 --- and must currently be `nil` or `0` or `{}`. 4417 --- 4418 --- Short reads are not possible. When less than `len` random bytes are available, 4419 --- a non-zero error value is returned or passed to the callback. If the callback 4420 --- is omitted, this function is completed synchronously. 4421 --- 4422 --- The synchronous version may block indefinitely when not enough entropy is 4423 --- available. The asynchronous version may not ever finish when the system is 4424 --- low on entropy. 4425 --- @param len integer 4426 --- @param flags 0|{}? 4427 --- @return string? bytes 4428 --- @return string? err 4429 --- @return uv.error_name? err_name 4430 --- @overload fun(len: integer, flags: 0|{}?, callback: fun(err: string?, bytes: string?)): 0?, string?, uv.error_name? 4431 function uv.random(len, flags) end 4432 4433 --- Returns the libuv error message and error name (both in string form, see [`err` and `name` in Error Handling](#error-handling)) equivalent to the given platform dependent error code: POSIX error codes on Unix (the ones stored in errno), and Win32 error codes on Windows (those returned by GetLastError() or WSAGetLastError()). 4434 --- @param errcode integer 4435 --- @return string? message 4436 --- @return string? name 4437 function uv.translate_sys_error(errcode) end 4438 4439 4440 --- # Metrics operations 4441 4442 --- Retrieve the amount of time the event loop has been idle in the kernel’s event 4443 --- provider (e.g. `epoll_wait`). The call is thread safe. 4444 --- 4445 --- The return value is the accumulated time spent idle in the kernel’s event 4446 --- provider starting from when the [`uv_loop_t`][] was configured to collect the idle time. 4447 --- 4448 --- **Note:** The event loop will not begin accumulating the event provider’s idle 4449 --- time until calling `loop_configure` with `"metrics_idle_time"`. 4450 --- @return number 4451 function uv.metrics_idle_time() end 4452 4453 --- @class uv.metrics_info.info 4454 --- @field loop_count number 4455 --- @field events integer 4456 --- @field events_waiting number 4457 4458 --- Get the metrics table from current set of event loop metrics. It is recommended 4459 --- to retrieve these metrics in a `prepare` callback (see `uv.new_prepare`, 4460 --- `uv.prepare_start`) in order to make sure there are no inconsistencies with the 4461 --- metrics counters. 4462 --- @return uv.metrics_info.info info 4463 function uv.metrics_info() end 4464 4465 4466 --- # String manipulation functions 4467 --- 4468 --- These string utilities are needed internally for dealing with Windows, and are exported to allow clients to work uniformly with this data when the libuv API is not complete. 4469 --- 4470 --- **Notes**: 4471 --- 4472 --- 1. New in luv version 1.49.0. 4473 --- 2. See [the WTF-8 spec](https://simonsapin.github.io/wtf-8/) for information about WTF-8. 4474 --- 3. Luv uses Lua-style strings, which means that all inputs and return values (UTF-8 or UTF-16 strings) do not include a NUL terminator. 4475 4476 --- Get the length (in bytes) of a UTF-16 (or UCS-2) string `utf16` value after converting it to WTF-8. 4477 --- @param utf16 string 4478 --- @return integer 4479 function uv.utf16_length_as_wtf8(utf16) end 4480 4481 --- Convert UTF-16 (or UCS-2) string `utf16` to WTF-8 string. The endianness of the UTF-16 (or UCS-2) string is assumed to be the same as the native endianness of the platform. 4482 --- @param utf16 string 4483 --- @return string 4484 function uv.utf16_to_wtf8(utf16) end 4485 4486 --- Get the length (in UTF-16 code units) of a WTF-8 `wtf8` value after converting it to UTF-16 (or UCS-2). Note: The number of bytes needed for a UTF-16 (or UCS-2) string is `<number of code units> * 2`. 4487 --- @param wtf8 string 4488 --- @return integer 4489 function uv.wtf8_length_as_utf16(wtf8) end 4490 4491 --- Convert WTF-8 string in `wtf8` to UTF-16 (or UCS-2) string. The endianness of the UTF-16 (or UCS-2) string will be the same as the native endianness of the platform. 4492 --- @param wtf8 string 4493 --- @return string 4494 function uv.wtf8_to_utf16(wtf8) end 4495 4496 4497 --- --- 4498 --- 4499 --- [luv]: https://github.com/luvit/luv 4500 --- [luvit]: https://github.com/luvit/luvit 4501 --- [libuv]: https://github.com/libuv/libuv 4502 --- [libuv documentation page]: http://docs.libuv.org/ 4503 --- [libuv API documentation]: http://docs.libuv.org/en/v1.x/api.html 4504 --- [error constants]: https://docs.libuv.org/en/v1.x/errors.html#error-constants 4505 4506 4507 --- @class uv.address 4508 --- @field addr string 4509 --- @field family string 4510 --- @field port integer? 4511 --- @field socktype string 4512 --- @field protocol string 4513 --- @field canonname string? 4514 4515 --- @alias uv.buffer 4516 --- | string 4517 --- | string[] 4518 4519 --- @class uv.socketinfo 4520 --- @field ip string 4521 --- @field family string 4522 --- @field port integer 4523 4524 --- @alias uv.threadargs 4525 --- | number 4526 --- | boolean 4527 --- | string 4528 --- | userdata 4529 4530 --- @class uv.uv_connect_t : uv.uv_req_t 4531 4532 --- @class uv.uv_fs_t : uv.uv_req_t 4533 4534 --- @class uv.uv_getaddrinfo_t : uv.uv_req_t 4535 4536 --- @class uv.uv_getnameinfo_t : uv.uv_req_t 4537 4538 --- @class uv.uv_shutdown_t : uv.uv_req_t 4539 4540 --- @class uv.uv_udp_send_t : uv.uv_req_t 4541 4542 --- @class uv.uv_work_t : uv.uv_req_t 4543 4544 --- @class uv.uv_write_t : uv.uv_req_t