@fuman/bun 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/connection.cjs +7 -0
- package/connection.d.ts +7 -4
- package/connection.js +7 -0
- package/listener.cjs +1 -0
- package/listener.d.ts +1 -0
- package/listener.js +1 -0
- package/package.json +4 -4
package/connection.cjs
CHANGED
|
@@ -4,12 +4,14 @@ const io = require("@fuman/io");
|
|
|
4
4
|
const net = require("@fuman/net");
|
|
5
5
|
const utils = require("@fuman/utils");
|
|
6
6
|
class TcpConnection {
|
|
7
|
+
/** Underlying socket */
|
|
7
8
|
socket;
|
|
8
9
|
#error = null;
|
|
9
10
|
#recvBuffer = io.Bytes.alloc(1024 * 16);
|
|
10
11
|
#sendBuffer = new utils.Deque();
|
|
11
12
|
#cv = new utils.ConditionVariable();
|
|
12
13
|
#endpoint;
|
|
14
|
+
/** Connect to the given endpoint (must be called as the first thing before using the connection) */
|
|
13
15
|
async connect(endpoint, tls = false) {
|
|
14
16
|
this.socket = await Bun.connect({
|
|
15
17
|
hostname: endpoint.address,
|
|
@@ -24,19 +26,23 @@ class TcpConnection {
|
|
|
24
26
|
});
|
|
25
27
|
this.#endpoint = endpoint;
|
|
26
28
|
}
|
|
29
|
+
/** Create a new TcpConnection from an existing socket */
|
|
27
30
|
static from(socket) {
|
|
28
31
|
const conn = new TcpConnection();
|
|
29
32
|
conn.socket = socket;
|
|
30
33
|
return conn;
|
|
31
34
|
}
|
|
35
|
+
/** @internal */
|
|
32
36
|
_handleData(_, data) {
|
|
33
37
|
this.#recvBuffer.writeSync(data.length).set(data);
|
|
34
38
|
this.#cv.notify();
|
|
35
39
|
}
|
|
40
|
+
/** @internal */
|
|
36
41
|
_handleError(_, error) {
|
|
37
42
|
this.#error = error;
|
|
38
43
|
this.#cv.notify();
|
|
39
44
|
}
|
|
45
|
+
/** @internal */
|
|
40
46
|
_handleClose() {
|
|
41
47
|
this.#error = new net.ConnectionClosedError();
|
|
42
48
|
this.#cv.notify();
|
|
@@ -44,6 +50,7 @@ class TcpConnection {
|
|
|
44
50
|
deferred.reject(this.#error);
|
|
45
51
|
}
|
|
46
52
|
}
|
|
53
|
+
/** @internal */
|
|
47
54
|
_handleDrain() {
|
|
48
55
|
while (!this.#sendBuffer.isEmpty()) {
|
|
49
56
|
const [chunk, deferred] = this.#sendBuffer.popFront();
|
package/connection.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { ITcpConnection, ITlsConnection, TcpEndpoint } from '@fuman/net';
|
|
2
2
|
import { Socket } from 'bun';
|
|
3
|
+
/**
|
|
4
|
+
* Implementation of {@link ITcpConnection} and {@link ITlsConnection} interfaces
|
|
5
|
+
* using Bun's `connect` function.
|
|
6
|
+
*/
|
|
3
7
|
export declare class TcpConnection implements ITcpConnection, ITlsConnection {
|
|
4
8
|
#private;
|
|
9
|
+
/** Underlying socket */
|
|
5
10
|
readonly socket: Socket<any>;
|
|
11
|
+
/** Connect to the given endpoint (must be called as the first thing before using the connection) */
|
|
6
12
|
connect(endpoint: TcpEndpoint, tls?: boolean): Promise<void>;
|
|
13
|
+
/** Create a new TcpConnection from an existing socket */
|
|
7
14
|
static from(socket: Socket<any>): TcpConnection;
|
|
8
|
-
_handleData(_: unknown, data: Buffer): void;
|
|
9
|
-
_handleError(_: unknown, error: Error): void;
|
|
10
|
-
_handleClose(): void;
|
|
11
|
-
_handleDrain(): void;
|
|
12
15
|
read(into: Uint8Array): Promise<number>;
|
|
13
16
|
write(bytes: Uint8Array): Promise<void>;
|
|
14
17
|
close(): void;
|
package/connection.js
CHANGED
|
@@ -2,12 +2,14 @@ import { Bytes } from "@fuman/io";
|
|
|
2
2
|
import { ConnectionClosedError } from "@fuman/net";
|
|
3
3
|
import { Deque, ConditionVariable, Deferred } from "@fuman/utils";
|
|
4
4
|
class TcpConnection {
|
|
5
|
+
/** Underlying socket */
|
|
5
6
|
socket;
|
|
6
7
|
#error = null;
|
|
7
8
|
#recvBuffer = Bytes.alloc(1024 * 16);
|
|
8
9
|
#sendBuffer = new Deque();
|
|
9
10
|
#cv = new ConditionVariable();
|
|
10
11
|
#endpoint;
|
|
12
|
+
/** Connect to the given endpoint (must be called as the first thing before using the connection) */
|
|
11
13
|
async connect(endpoint, tls = false) {
|
|
12
14
|
this.socket = await Bun.connect({
|
|
13
15
|
hostname: endpoint.address,
|
|
@@ -22,19 +24,23 @@ class TcpConnection {
|
|
|
22
24
|
});
|
|
23
25
|
this.#endpoint = endpoint;
|
|
24
26
|
}
|
|
27
|
+
/** Create a new TcpConnection from an existing socket */
|
|
25
28
|
static from(socket) {
|
|
26
29
|
const conn = new TcpConnection();
|
|
27
30
|
conn.socket = socket;
|
|
28
31
|
return conn;
|
|
29
32
|
}
|
|
33
|
+
/** @internal */
|
|
30
34
|
_handleData(_, data) {
|
|
31
35
|
this.#recvBuffer.writeSync(data.length).set(data);
|
|
32
36
|
this.#cv.notify();
|
|
33
37
|
}
|
|
38
|
+
/** @internal */
|
|
34
39
|
_handleError(_, error) {
|
|
35
40
|
this.#error = error;
|
|
36
41
|
this.#cv.notify();
|
|
37
42
|
}
|
|
43
|
+
/** @internal */
|
|
38
44
|
_handleClose() {
|
|
39
45
|
this.#error = new ConnectionClosedError();
|
|
40
46
|
this.#cv.notify();
|
|
@@ -42,6 +48,7 @@ class TcpConnection {
|
|
|
42
48
|
deferred.reject(this.#error);
|
|
43
49
|
}
|
|
44
50
|
}
|
|
51
|
+
/** @internal */
|
|
45
52
|
_handleDrain() {
|
|
46
53
|
while (!this.#sendBuffer.isEmpty()) {
|
|
47
54
|
const [chunk, deferred] = this.#sendBuffer.popFront();
|
package/listener.cjs
CHANGED
package/listener.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { TcpConnection } from './connection.js';
|
|
|
3
3
|
export declare class TcpListener implements IListener<TcpEndpoint, TcpConnection> {
|
|
4
4
|
#private;
|
|
5
5
|
constructor();
|
|
6
|
+
/** listen on the given endpoint; must be called before any other methods */
|
|
6
7
|
listen(endpoint: TcpEndpoint): void;
|
|
7
8
|
close(): void;
|
|
8
9
|
get address(): TcpEndpoint;
|
package/listener.js
CHANGED
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuman/bun",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "bun-specific utilities",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@fuman/io": "^0.0.
|
|
10
|
-
"@fuman/net": "^0.0.
|
|
11
|
-
"@fuman/utils": "^0.0.
|
|
9
|
+
"@fuman/io": "^0.0.3",
|
|
10
|
+
"@fuman/net": "^0.0.3",
|
|
11
|
+
"@fuman/utils": "^0.0.3"
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|