diff options
Diffstat (limited to 'packages')
-rwxr-xr-x | packages/bun-internal-test/bun.lockb | bin | 3218 -> 3658 bytes | |||
-rw-r--r-- | packages/bun-types/bun.d.ts | 37 | ||||
-rw-r--r-- | packages/bun-types/globals.d.ts | 2 | ||||
-rw-r--r-- | packages/bun-usockets/src/bsd.c | 51 | ||||
-rw-r--r-- | packages/bun-usockets/src/socket.c | 2 |
5 files changed, 76 insertions, 16 deletions
diff --git a/packages/bun-internal-test/bun.lockb b/packages/bun-internal-test/bun.lockb Binary files differindex f0d6d2032..cef9d5e7e 100755 --- a/packages/bun-internal-test/bun.lockb +++ b/packages/bun-internal-test/bun.lockb diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index a2ccebd8a..9b2d314e8 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -2359,11 +2359,7 @@ declare module "bun" { ): ServerWebSocketSendStatus; /** - * Returns the client IP address of the given Request. - * - * @param request The incoming request - * - * @returns An ipv4/ipv6 address string, or null if it couldn't find one. + * Returns the client IP address and port of the given Request. If the request was closed or is a unix socket, returns null. * * @example * ```js @@ -3297,6 +3293,37 @@ declare module "bun" { * The config object passed to `Bun.build` as is. Can be mutated. */ config: BuildConfig & { plugins: BunPlugin[] }; + + /** + * Create a lazy-loaded virtual module that can be `import`ed or `require`d from other modules + * + * @param specifier The module specifier to register the callback for + * @param callback The function to run when the module is imported or required + * + * ### Example + * @example + * ```ts + * Bun.plugin({ + * setup(builder) { + * builder.module("hello:world", () => { + * return { exports: { foo: "bar" }, loader: "object" }; + * }); + * }, + * }); + * + * // sometime later + * const { foo } = await import("hello:world"); + * console.log(foo); // "bar" + * + * // or + * const { foo } = require("hello:world"); + * console.log(foo); // "bar" + * ``` + */ + module( + specifier: string, + callback: () => OnLoadResult | Promise<OnLoadResult>, + ): void; } interface BunPlugin { diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index a1f1bd325..ea73b506d 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -681,7 +681,7 @@ interface Process { */ setSourceMapsEnabled(enabled: boolean): void; - kill(pid: number, signal?: string | number): void; + kill(pid: number, signal?: string | number): true; on(event: "beforeExit", listener: BeforeExitListener): this; // on(event: "disconnect", listener: DisconnectListener): this; diff --git a/packages/bun-usockets/src/bsd.c b/packages/bun-usockets/src/bsd.c index 7683acd7d..fc501e4d9 100644 --- a/packages/bun-usockets/src/bsd.c +++ b/packages/bun-usockets/src/bsd.c @@ -665,9 +665,39 @@ int bsd_udp_packet_buffer_ecn(void *msgvec, int index) { return 0; // no ecn defaults to 0 } -static int bsd_do_connect(struct addrinfo *result, int fd) +static int bsd_do_connect_raw(struct addrinfo *rp, int fd) { - return connect(fd, result->ai_addr, (socklen_t) result->ai_addrlen); + do { + if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0 || errno == EINPROGRESS) { + return 0; + } + } while (errno == EINTR); + + return LIBUS_SOCKET_ERROR; +} + +static int bsd_do_connect(struct addrinfo *rp, int *fd) +{ + while (rp != NULL) { + if (bsd_do_connect_raw(rp, *fd) == 0) { + return 0; + } + + rp = rp->ai_next; + bsd_close_socket(*fd); + + if (rp == NULL) { + return LIBUS_SOCKET_ERROR; + } + + int resultFd = bsd_create_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (resultFd < 0) { + return LIBUS_SOCKET_ERROR; + } + *fd = resultFd; + } + + return LIBUS_SOCKET_ERROR; } LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, const char *source_host, int options) { @@ -700,18 +730,21 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, co return LIBUS_SOCKET_ERROR; } } - } - - do { - if (bsd_do_connect(result, fd) != 0 && errno != EINPROGRESS) { + + if (bsd_do_connect_raw(result, fd) != 0) { bsd_close_socket(fd); freeaddrinfo(result); return LIBUS_SOCKET_ERROR; } - } while (errno == EINTR); - + } else { + if (bsd_do_connect(result, &fd) != 0) { + freeaddrinfo(result); + return LIBUS_SOCKET_ERROR; + } + } + + freeaddrinfo(result); - return fd; } diff --git a/packages/bun-usockets/src/socket.c b/packages/bun-usockets/src/socket.c index 78185e681..6edefe0f5 100644 --- a/packages/bun-usockets/src/socket.c +++ b/packages/bun-usockets/src/socket.c @@ -290,7 +290,7 @@ unsigned int us_get_remote_address_info(char *buf, struct us_socket_t *s, const struct bsd_addr_t addr; if (bsd_remote_addr(us_poll_fd(&s->p), &addr)) { return 0; - } + } int length = bsd_addr_get_ip_length(&addr); if (!length) { |