mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
add some connect test and type changes (#3013)
This commit is contained in:
4
packages/bun-types/bun.d.ts
vendored
4
packages/bun-types/bun.d.ts
vendored
@@ -3139,10 +3139,10 @@ declare module "bun" {
|
||||
*/
|
||||
export function connect<Data = undefined>(
|
||||
options: TCPSocketConnectOptions<Data>,
|
||||
): Promise<TCPSocketListener<typeof options>>;
|
||||
): Promise<Socket<Data>>;
|
||||
export function connect<Data = undefined>(
|
||||
options: UnixSocketOptions<Data>,
|
||||
): Promise<UnixSocketListener<typeof options>>;
|
||||
): Promise<Socket<Data>>;
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
53
test/js/bun/net/connect-returns-socket-unix.js
Normal file
53
test/js/bun/net/connect-returns-socket-unix.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
|
||||
let resolve;
|
||||
const promise = new Promise(r => (resolve = r));
|
||||
|
||||
const unix = os.tmpdir() + "/bun-connect-unix-socket-test.socket";
|
||||
|
||||
const server = Bun.listen({
|
||||
unix,
|
||||
socket: {
|
||||
open(socket) {
|
||||
console.log("SERVER OPENED");
|
||||
},
|
||||
data(socket, buffer) {
|
||||
socket.write(buffer);
|
||||
},
|
||||
error(socket, err) {
|
||||
console.log("SERVER ERRED", err);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const client = await Bun.connect({
|
||||
unix,
|
||||
socket: {
|
||||
open(socket) {
|
||||
console.log("CLIENT OPENED");
|
||||
socket.write("Hello, world!");
|
||||
},
|
||||
data(socket, buffer) {
|
||||
console.log("CLIENT RECEIVED", buffer.toString());
|
||||
if (buffer.toString().includes("From returned socket")) {
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(client.localPort);
|
||||
client.write("From returned socket");
|
||||
|
||||
setTimeout(() => {
|
||||
console.error("Test Failed");
|
||||
process.exit(1);
|
||||
}, 1000);
|
||||
|
||||
await promise;
|
||||
|
||||
client.end();
|
||||
server.stop();
|
||||
|
||||
process.exit(0);
|
||||
50
test/js/bun/net/connect-returns-socket.js
Normal file
50
test/js/bun/net/connect-returns-socket.js
Normal file
@@ -0,0 +1,50 @@
|
||||
let resolve;
|
||||
const promise = new Promise(r => (resolve = r));
|
||||
|
||||
const server = Bun.listen({
|
||||
hostname: "localhost",
|
||||
port: 0,
|
||||
socket: {
|
||||
open(socket) {
|
||||
console.log("SERVER OPENED");
|
||||
},
|
||||
data(socket, buffer) {
|
||||
socket.write(buffer);
|
||||
},
|
||||
error(socket, err) {
|
||||
console.log("SERVER ERRED", err);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const client = await Bun.connect({
|
||||
hostname: "localhost",
|
||||
port: server.port,
|
||||
socket: {
|
||||
open(socket) {
|
||||
console.log("CLIENT OPENED");
|
||||
socket.write("Hello, world!");
|
||||
},
|
||||
data(socket, buffer) {
|
||||
console.log("CLIENT RECEIVED", buffer.toString());
|
||||
if (buffer.toString().includes("From returned socket")) {
|
||||
resolve();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(client.localPort);
|
||||
client.write("From returned socket");
|
||||
|
||||
setTimeout(() => {
|
||||
console.error("Test Failed");
|
||||
process.exit(1);
|
||||
}, 1000);
|
||||
|
||||
await promise;
|
||||
|
||||
client.end();
|
||||
server.stop();
|
||||
|
||||
process.exit(0);
|
||||
@@ -74,7 +74,7 @@ const server = Bun.listen(
|
||||
}),
|
||||
);
|
||||
|
||||
await Bun.connect({
|
||||
const socket = await Bun.connect({
|
||||
...createOptions("[Client]", "request"),
|
||||
port: server.port,
|
||||
});
|
||||
|
||||
@@ -27,6 +27,20 @@ it("should keep process alive only when active", async () => {
|
||||
).toEqual(["[Client] OPENED", "[Client] GOT response", "[Client] CLOSED"]);
|
||||
});
|
||||
|
||||
it("connect() should return the socket object", async () => {
|
||||
const { exited, stdout, stderr } = spawn({
|
||||
cmd: [bunExe(), "connect-returns-socket.js"],
|
||||
cwd: import.meta.dir,
|
||||
stdout: "pipe",
|
||||
stdin: null,
|
||||
stderr: "pipe",
|
||||
env: bunEnv,
|
||||
});
|
||||
|
||||
expect(await exited).toBe(0);
|
||||
expect(await new Response(stderr).text()).toBe("");
|
||||
});
|
||||
|
||||
it("listen() should throw connection error for invalid host", () => {
|
||||
expect(() => {
|
||||
const handlers: SocketHandler = {
|
||||
|
||||
Reference in New Issue
Block a user