@6qat/tcp-connection 0.2.0 → 0.2.2
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/dist/index.js +39 -2
- package/dist/tcp-stream.d.ts +1 -2
- package/package.json +9 -7
- package/dist/tcp-stream.js +0 -82
package/dist/tcp-stream.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, Stream, Duration } from "effect";
|
|
2
|
-
interface TcpConnection {
|
|
2
|
+
export interface TcpConnection {
|
|
3
3
|
readonly stream: Stream.Stream<Uint8Array, Error>;
|
|
4
4
|
readonly send: (data: Uint8Array) => Effect.Effect<void>;
|
|
5
5
|
readonly sendText: (data: string) => Effect.Effect<void>;
|
|
@@ -10,4 +10,3 @@ export declare const createTcpConnection: (options: {
|
|
|
10
10
|
port: number;
|
|
11
11
|
timeout?: Duration.Duration;
|
|
12
12
|
}) => Effect.Effect<TcpConnection, Error>;
|
|
13
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@6qat/tcp-connection",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "TCP connection library with Effect.js integration",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,19 +8,21 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"files": ["dist", "README.md"],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "
|
|
12
|
-
"
|
|
13
|
-
"
|
|
11
|
+
"build": "bun build --minify-syntax --minify-whitespace ./src/index.ts --outdir ./dist --target node --format esm",
|
|
12
|
+
"build:types": "bun tsc --emitDeclarationOnly --outDir dist",
|
|
13
|
+
"build:all": "bun run build && bun run build:types",
|
|
14
|
+
"prepublishOnly": "bun run build:all",
|
|
15
|
+
"test": "bun test"
|
|
14
16
|
},
|
|
15
17
|
"keywords": ["tcp", "connection", "effect", "bun"],
|
|
16
18
|
"author": "Your Name",
|
|
17
19
|
"license": "MIT",
|
|
18
20
|
"devDependencies": {
|
|
19
|
-
"@types/bun": "
|
|
20
|
-
"typescript": "^5"
|
|
21
|
+
"@types/bun": "^1.2.11",
|
|
22
|
+
"typescript": "^5.8.3"
|
|
21
23
|
},
|
|
22
24
|
"peerDependencies": {
|
|
23
|
-
"effect": "
|
|
25
|
+
"effect": "3.14.18"
|
|
24
26
|
},
|
|
25
27
|
"publishConfig": {
|
|
26
28
|
"access": "public"
|
package/dist/tcp-stream.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Effect, Stream, Queue, pipe, Fiber, Duration, Ref } from "effect";
|
|
2
|
-
export const createTcpConnection = (options) => {
|
|
3
|
-
return Effect.scoped(Effect.gen(function* () {
|
|
4
|
-
// Create queues for incoming and outgoing data
|
|
5
|
-
const incomingQueue = yield* Queue.unbounded();
|
|
6
|
-
const outgoingQueue = yield* Queue.unbounded();
|
|
7
|
-
// Use refs for coordinated cleanup
|
|
8
|
-
const isClosing = yield* Ref.make(false);
|
|
9
|
-
// Track error count
|
|
10
|
-
const writeErrorCount = yield* Ref.make(0);
|
|
11
|
-
// Safely shut down once
|
|
12
|
-
const performShutdown = yield* Effect.cached(Effect.gen(function* () {
|
|
13
|
-
const alreadyClosing = yield* Ref.getAndSet(isClosing, true);
|
|
14
|
-
if (alreadyClosing)
|
|
15
|
-
return;
|
|
16
|
-
bunSocket.end();
|
|
17
|
-
// Allow socket events to propagate
|
|
18
|
-
yield* Effect.sleep(Duration.millis(10));
|
|
19
|
-
// Interrupt writer fiber before shutting down queues
|
|
20
|
-
yield* Fiber.interrupt(writerFiber);
|
|
21
|
-
yield* Effect.all([
|
|
22
|
-
Queue.shutdown(incomingQueue),
|
|
23
|
-
Queue.shutdown(outgoingQueue),
|
|
24
|
-
]);
|
|
25
|
-
}));
|
|
26
|
-
// Create deferred for connection cleanup
|
|
27
|
-
const bunSocket = yield* Effect.tryPromise(() => Bun.connect({
|
|
28
|
-
port: options.port,
|
|
29
|
-
hostname: options.host,
|
|
30
|
-
socket: {
|
|
31
|
-
data(_socket, data) {
|
|
32
|
-
Queue.unsafeOffer(incomingQueue, data);
|
|
33
|
-
},
|
|
34
|
-
error(_socket, error) {
|
|
35
|
-
//Queue.unsafeOffer(incomingQueue, error)
|
|
36
|
-
Effect.runSync(performShutdown);
|
|
37
|
-
},
|
|
38
|
-
close(_socket) {
|
|
39
|
-
Effect.runSync(performShutdown);
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
})).pipe(Effect.timeout(options.timeout ?? Duration.millis(3000)), Effect.flatMap((maybeSocket) => maybeSocket
|
|
43
|
-
? Effect.succeed(maybeSocket)
|
|
44
|
-
: Effect.fail(new Error("Connection timeout"))));
|
|
45
|
-
// Fiber for writing outgoing data
|
|
46
|
-
const writerFiber = yield* pipe(Effect.iterate(undefined, {
|
|
47
|
-
while: () => true,
|
|
48
|
-
body: () => pipe(Queue.take(outgoingQueue), Effect.flatMap((data) => Effect.try({
|
|
49
|
-
try: () => {
|
|
50
|
-
const bytesWritten = bunSocket.write(data);
|
|
51
|
-
if (bytesWritten !== data.length) {
|
|
52
|
-
throw new Error("Partial write");
|
|
53
|
-
}
|
|
54
|
-
// Reset error count on success
|
|
55
|
-
Effect.runSync(Ref.set(writeErrorCount, 0));
|
|
56
|
-
},
|
|
57
|
-
catch: (error) => {
|
|
58
|
-
const currentErrors = Effect.runSync(Ref.updateAndGet(writeErrorCount, (n) => n + 1));
|
|
59
|
-
if (currentErrors > 3) {
|
|
60
|
-
// Too many errors, close the socket
|
|
61
|
-
Effect.runSync(performShutdown);
|
|
62
|
-
return Effect.fail(new Error(`Write failed after ${currentErrors} attempts: ${error}`));
|
|
63
|
-
}
|
|
64
|
-
// Retry with the same data after a delay
|
|
65
|
-
return Effect.sleep(Duration.millis(100)).pipe(Effect.flatMap(() => Queue.offer(outgoingQueue, data)));
|
|
66
|
-
},
|
|
67
|
-
}))),
|
|
68
|
-
}), Effect.forkScoped);
|
|
69
|
-
// Cleanup procedure
|
|
70
|
-
const close = Effect.gen(function* () {
|
|
71
|
-
console.log("Closing connection");
|
|
72
|
-
yield* performShutdown;
|
|
73
|
-
});
|
|
74
|
-
// returns TCPConnection
|
|
75
|
-
return {
|
|
76
|
-
stream: Stream.fromQueue(incomingQueue).pipe(Stream.ensuring(close)),
|
|
77
|
-
send: (data) => Queue.offer(outgoingQueue, data),
|
|
78
|
-
sendText: (data) => Queue.offer(outgoingQueue, new TextEncoder().encode(data)),
|
|
79
|
-
close,
|
|
80
|
-
};
|
|
81
|
-
}));
|
|
82
|
-
};
|