@6qat/tcp-connection 0.1.0

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/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # TCP Connection
2
+
3
+ A robust TCP connection library with Effect.js integration.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @6qat/tcp-connection
9
+ # or
10
+ npm install @6qat/tcp-connection
11
+ # or
12
+ yarn add @6qat/tcp-connection
@@ -0,0 +1 @@
1
+ export * from "./tcp-stream";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // Export everything from tcp-stream
2
+ export * from "./tcp-stream";
@@ -0,0 +1,13 @@
1
+ import { Effect, Stream, Duration } from "effect";
2
+ interface TcpConnection {
3
+ readonly stream: Stream.Stream<Uint8Array, Error>;
4
+ readonly send: (data: Uint8Array) => Effect.Effect<void>;
5
+ readonly sendText: (data: string) => Effect.Effect<void>;
6
+ readonly close: Effect.Effect<void>;
7
+ }
8
+ export declare const createTcpConnection: (options: {
9
+ host: string;
10
+ port: number;
11
+ timeout?: Duration.Duration;
12
+ }) => Effect.Effect<TcpConnection, Error>;
13
+ export {};
@@ -0,0 +1,56 @@
1
+ import { Effect, Stream, Queue, pipe, Fiber, Duration } from "effect";
2
+ export const createTcpConnection = (options) => {
3
+ return Effect.gen(function* () {
4
+ // Create queues for incoming and outgoing data
5
+ const incomingQueue = yield* Queue.unbounded();
6
+ const outgoingQueue = yield* Queue.unbounded();
7
+ // Create deferred for connection cleanup
8
+ const socketEffect = yield* Effect.tryPromise(() => Bun.connect({
9
+ port: options.port,
10
+ hostname: options.host,
11
+ socket: {
12
+ data(_socket, data) {
13
+ Queue.unsafeOffer(incomingQueue, data);
14
+ },
15
+ error(_socket, error) {
16
+ //Queue.unsafeOffer(incomingQueue, error)
17
+ Queue.shutdown(outgoingQueue);
18
+ },
19
+ close(_socket) {
20
+ Queue.shutdown(incomingQueue);
21
+ Queue.shutdown(outgoingQueue);
22
+ },
23
+ },
24
+ })).pipe(Effect.timeout(options.timeout ?? Duration.millis(3000)), Effect.flatMap((maybeSocket) => maybeSocket
25
+ ? Effect.succeed(maybeSocket)
26
+ : Effect.fail(new Error("Connection timeout"))));
27
+ // Fiber for writing outgoing data
28
+ const writerFiber = yield* pipe(Effect.iterate(undefined, {
29
+ while: () => true,
30
+ body: () => pipe(Queue.take(outgoingQueue), Effect.flatMap((data) => Effect.try({
31
+ try: () => {
32
+ const bytesWritten = socketEffect.write(data);
33
+ if (bytesWritten !== data.length) {
34
+ throw new Error("Partial write");
35
+ }
36
+ },
37
+ catch: (error) => new Error(`Write failed: ${error}`),
38
+ }))),
39
+ }), Effect.fork);
40
+ // Cleanup procedure
41
+ const close = Effect.gen(function* () {
42
+ console.log("Closing connection");
43
+ socketEffect.end();
44
+ yield* Queue.shutdown(incomingQueue);
45
+ yield* Queue.shutdown(outgoingQueue);
46
+ yield* Fiber.interrupt(writerFiber);
47
+ });
48
+ // returns TCPConnection
49
+ return {
50
+ stream: Stream.fromQueue(incomingQueue).pipe(Stream.ensuring(close)),
51
+ send: (data) => Queue.offer(outgoingQueue, data),
52
+ sendText: (data) => Queue.offer(outgoingQueue, new TextEncoder().encode(data)),
53
+ close,
54
+ };
55
+ });
56
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@6qat/tcp-connection",
3
+ "version": "0.1.0",
4
+ "description": "TCP connection library with Effect.js integration",
5
+ "module": "dist/index.js",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "type": "module",
9
+ "files": ["dist", "README.md"],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "prepublishOnly": "npm run build",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "keywords": ["tcp", "connection", "effect", "bun"],
16
+ "author": "Your Name",
17
+ "license": "MIT",
18
+ "devDependencies": {
19
+ "@types/bun": "latest",
20
+ "typescript": "^5"
21
+ },
22
+ "peerDependencies": {
23
+ "effect": "^2.0.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }