@6qat/tcp-connection 0.2.1 → 0.2.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/dist/index.d.ts +1 -1
- package/dist/index.js +39 -2
- package/dist/tcp-connection.d.ts +33 -0
- package/dist/tcp-connection.test.d.ts +1 -0
- package/dist/tcp-stream.d.ts +2 -3
- package/package.json +11 -7
- package/dist/tcp-stream.js +0 -82
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Effect, Stream, Duration } from 'effect';
|
|
2
|
+
import type { TimeoutException } from 'effect/Cause';
|
|
3
|
+
declare const TcpConnectionError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
4
|
+
readonly _tag: "TcpConnectionError";
|
|
5
|
+
} & Readonly<A>;
|
|
6
|
+
export declare class TcpConnectionError extends TcpConnectionError_base {
|
|
7
|
+
readonly errorType?: string | undefined;
|
|
8
|
+
constructor(errorType?: string | undefined);
|
|
9
|
+
}
|
|
10
|
+
export declare class BunError extends TcpConnectionError {
|
|
11
|
+
readonly message: string;
|
|
12
|
+
readonly errorType = "BunError";
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
export interface TcpStreamConfig {
|
|
16
|
+
readonly host: string;
|
|
17
|
+
readonly port: number;
|
|
18
|
+
readonly bufferSize?: number;
|
|
19
|
+
readonly connectTimeout?: Duration.DurationInput;
|
|
20
|
+
}
|
|
21
|
+
export declare class TcpStream {
|
|
22
|
+
private readonly incomingQueue;
|
|
23
|
+
private readonly outgoingQueue;
|
|
24
|
+
private readonly performShutdown;
|
|
25
|
+
private constructor();
|
|
26
|
+
static connect(config: TcpStreamConfig): Effect.Effect<TcpStream, TcpConnectionError | TimeoutException>;
|
|
27
|
+
get incomingStream(): Stream.Stream<Uint8Array, TcpConnectionError>;
|
|
28
|
+
write(data: Uint8Array): Effect.Effect<boolean, TcpConnectionError>;
|
|
29
|
+
writeText(data: string): Effect.Effect<boolean, TcpConnectionError>;
|
|
30
|
+
close(): Effect.Effect<void>;
|
|
31
|
+
static managedStream(config: TcpStreamConfig): Stream.Stream<Uint8Array, TcpConnectionError | TimeoutException>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/tcp-stream.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Effect, Stream, Duration } from
|
|
2
|
-
interface TcpConnection {
|
|
1
|
+
import { Effect, Stream, Duration } from 'effect';
|
|
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.3",
|
|
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,23 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"files": ["dist", "README.md"],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "
|
|
12
|
-
"
|
|
13
|
-
"
|
|
11
|
+
"build:main": "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": "bun run build:main && bun run build:types",
|
|
14
|
+
"prepublishOnly": "bun run build",
|
|
15
|
+
"test": "bun test",
|
|
16
|
+
"format": "biome format --write ./src",
|
|
17
|
+
"lint": "biome lint ."
|
|
14
18
|
},
|
|
15
19
|
"keywords": ["tcp", "connection", "effect", "bun"],
|
|
16
20
|
"author": "Your Name",
|
|
17
21
|
"license": "MIT",
|
|
18
22
|
"devDependencies": {
|
|
19
|
-
"@types/bun": "
|
|
20
|
-
"typescript": "^5"
|
|
23
|
+
"@types/bun": "^1.2.11",
|
|
24
|
+
"typescript": "^5.8.3"
|
|
21
25
|
},
|
|
22
26
|
"peerDependencies": {
|
|
23
|
-
"effect": "^
|
|
27
|
+
"effect": "^3.14.21"
|
|
24
28
|
},
|
|
25
29
|
"publishConfig": {
|
|
26
30
|
"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
|
-
// Track error count
|
|
8
|
-
const writeErrorCount = yield* Ref.make(0);
|
|
9
|
-
// Use refs for coordinated cleanup
|
|
10
|
-
const isClosing = yield* Ref.make(false);
|
|
11
|
-
// Safely shut down once
|
|
12
|
-
const performShutdown = 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.runPromise(performShutdown);
|
|
37
|
-
},
|
|
38
|
-
close(_socket) {
|
|
39
|
-
Effect.runPromise(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.fork);
|
|
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
|
-
};
|