@fuman/bun 0.0.11 → 0.0.14
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 +18 -8
- package/connection.js +18 -8
- package/package.json +4 -4
package/connection.cjs
CHANGED
|
@@ -3,6 +3,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const io = require("@fuman/io");
|
|
4
4
|
const net = require("@fuman/net");
|
|
5
5
|
const utils = require("@fuman/utils");
|
|
6
|
+
class SendBufferItem {
|
|
7
|
+
constructor(bytes, deferred) {
|
|
8
|
+
this.bytes = bytes;
|
|
9
|
+
this.deferred = deferred;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
6
12
|
class TcpConnection {
|
|
7
13
|
/** Underlying socket */
|
|
8
14
|
socket;
|
|
@@ -46,17 +52,19 @@ class TcpConnection {
|
|
|
46
52
|
_handleClose() {
|
|
47
53
|
this.#error = new net.ConnectionClosedError();
|
|
48
54
|
this.#cv.notify();
|
|
49
|
-
for (const
|
|
55
|
+
for (const { deferred } of this.#sendBuffer) {
|
|
50
56
|
deferred.reject(this.#error);
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
/** @internal */
|
|
54
60
|
_handleDrain() {
|
|
55
61
|
while (!this.#sendBuffer.isEmpty()) {
|
|
56
|
-
const
|
|
62
|
+
const item = this.#sendBuffer.popFront();
|
|
63
|
+
const { bytes: chunk, deferred } = item;
|
|
57
64
|
const written = this.socket.write(chunk);
|
|
58
65
|
if (written < chunk.length) {
|
|
59
|
-
|
|
66
|
+
item.bytes = chunk.subarray(written);
|
|
67
|
+
this.#sendBuffer.pushFront(item);
|
|
60
68
|
break;
|
|
61
69
|
}
|
|
62
70
|
deferred.resolve();
|
|
@@ -79,12 +87,14 @@ class TcpConnection {
|
|
|
79
87
|
}
|
|
80
88
|
async write(bytes) {
|
|
81
89
|
if (this.#error) throw this.#error;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return deferred.promise;
|
|
90
|
+
if (this.#sendBuffer.isEmpty()) {
|
|
91
|
+
const written = this.socket.write(bytes);
|
|
92
|
+
if (written === bytes.length) return;
|
|
93
|
+
bytes = bytes.subarray(written);
|
|
87
94
|
}
|
|
95
|
+
const deferred = new utils.Deferred();
|
|
96
|
+
this.#sendBuffer.pushBack(new SendBufferItem(bytes, deferred));
|
|
97
|
+
return deferred.promise;
|
|
88
98
|
}
|
|
89
99
|
close() {
|
|
90
100
|
this.socket.end();
|
package/connection.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Bytes } from "@fuman/io";
|
|
2
2
|
import { ConnectionClosedError } from "@fuman/net";
|
|
3
3
|
import { Deque, ConditionVariable, Deferred } from "@fuman/utils";
|
|
4
|
+
class SendBufferItem {
|
|
5
|
+
constructor(bytes, deferred) {
|
|
6
|
+
this.bytes = bytes;
|
|
7
|
+
this.deferred = deferred;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
4
10
|
class TcpConnection {
|
|
5
11
|
/** Underlying socket */
|
|
6
12
|
socket;
|
|
@@ -44,17 +50,19 @@ class TcpConnection {
|
|
|
44
50
|
_handleClose() {
|
|
45
51
|
this.#error = new ConnectionClosedError();
|
|
46
52
|
this.#cv.notify();
|
|
47
|
-
for (const
|
|
53
|
+
for (const { deferred } of this.#sendBuffer) {
|
|
48
54
|
deferred.reject(this.#error);
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
57
|
/** @internal */
|
|
52
58
|
_handleDrain() {
|
|
53
59
|
while (!this.#sendBuffer.isEmpty()) {
|
|
54
|
-
const
|
|
60
|
+
const item = this.#sendBuffer.popFront();
|
|
61
|
+
const { bytes: chunk, deferred } = item;
|
|
55
62
|
const written = this.socket.write(chunk);
|
|
56
63
|
if (written < chunk.length) {
|
|
57
|
-
|
|
64
|
+
item.bytes = chunk.subarray(written);
|
|
65
|
+
this.#sendBuffer.pushFront(item);
|
|
58
66
|
break;
|
|
59
67
|
}
|
|
60
68
|
deferred.resolve();
|
|
@@ -77,12 +85,14 @@ class TcpConnection {
|
|
|
77
85
|
}
|
|
78
86
|
async write(bytes) {
|
|
79
87
|
if (this.#error) throw this.#error;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return deferred.promise;
|
|
88
|
+
if (this.#sendBuffer.isEmpty()) {
|
|
89
|
+
const written = this.socket.write(bytes);
|
|
90
|
+
if (written === bytes.length) return;
|
|
91
|
+
bytes = bytes.subarray(written);
|
|
85
92
|
}
|
|
93
|
+
const deferred = new Deferred();
|
|
94
|
+
this.#sendBuffer.pushBack(new SendBufferItem(bytes, deferred));
|
|
95
|
+
return deferred.promise;
|
|
86
96
|
}
|
|
87
97
|
close() {
|
|
88
98
|
this.socket.end();
|
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.14",
|
|
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.14",
|
|
10
|
+
"@fuman/net": "^0.0.14",
|
|
11
|
+
"@fuman/utils": "^0.0.14"
|
|
12
12
|
},
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|