@6qat/tcp-connection 0.1.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/dist/tcp-stream.js +40 -14
  2. package/package.json +26 -26
@@ -1,24 +1,42 @@
1
- import { Effect, Stream, Queue, pipe, Fiber, Duration } from "effect";
1
+ import { Effect, Stream, Queue, pipe, Fiber, Duration, Ref } from "effect";
2
2
  export const createTcpConnection = (options) => {
3
- return Effect.gen(function* () {
3
+ return Effect.scoped(Effect.gen(function* () {
4
4
  // Create queues for incoming and outgoing data
5
5
  const incomingQueue = yield* Queue.unbounded();
6
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
+ });
7
26
  // Create deferred for connection cleanup
8
- const socketEffect = yield* Effect.tryPromise(() => Bun.connect({
27
+ const bunSocket = yield* Effect.tryPromise(() => Bun.connect({
9
28
  port: options.port,
10
29
  hostname: options.host,
11
30
  socket: {
12
31
  data(_socket, data) {
13
32
  Queue.unsafeOffer(incomingQueue, data);
14
33
  },
15
- error(_socket, error) {
34
+ error(_socket, _error) {
16
35
  //Queue.unsafeOffer(incomingQueue, error)
17
- Queue.shutdown(outgoingQueue);
36
+ Effect.runPromise(performShutdown);
18
37
  },
19
38
  close(_socket) {
20
- Queue.shutdown(incomingQueue);
21
- Queue.shutdown(outgoingQueue);
39
+ Effect.runPromise(performShutdown);
22
40
  },
23
41
  },
24
42
  })).pipe(Effect.timeout(options.timeout ?? Duration.millis(3000)), Effect.flatMap((maybeSocket) => maybeSocket
@@ -29,21 +47,29 @@ export const createTcpConnection = (options) => {
29
47
  while: () => true,
30
48
  body: () => pipe(Queue.take(outgoingQueue), Effect.flatMap((data) => Effect.try({
31
49
  try: () => {
32
- const bytesWritten = socketEffect.write(data);
50
+ const bytesWritten = bunSocket.write(data);
33
51
  if (bytesWritten !== data.length) {
34
52
  throw new Error("Partial write");
35
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)));
36
66
  },
37
- catch: (error) => new Error(`Write failed: ${error}`),
38
67
  }))),
39
68
  }), Effect.fork);
40
69
  // Cleanup procedure
41
70
  const close = Effect.gen(function* () {
42
71
  console.log("Closing connection");
43
- socketEffect.end();
44
- yield* Queue.shutdown(incomingQueue);
45
- yield* Queue.shutdown(outgoingQueue);
46
- yield* Fiber.interrupt(writerFiber);
72
+ yield* performShutdown;
47
73
  });
48
74
  // returns TCPConnection
49
75
  return {
@@ -52,5 +78,5 @@ export const createTcpConnection = (options) => {
52
78
  sendText: (data) => Queue.offer(outgoingQueue, new TextEncoder().encode(data)),
53
79
  close,
54
80
  };
55
- });
81
+ }));
56
82
  };
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
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
- }
2
+ "name": "@6qat/tcp-connection",
3
+ "version": "0.2.1",
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
28
  }