Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno.connect failed to read full bytes #28547

Closed
Srabutdotcom opened this issue Mar 19, 2025 · 6 comments
Closed

Deno.connect failed to read full bytes #28547

Srabutdotcom opened this issue Mar 19, 2025 · 6 comments

Comments

@Srabutdotcom
Copy link

Srabutdotcom commented Mar 19, 2025

Version: Deno 2.2.4

Expected output are:
"250-smtp.gmail.com at your servi.."
"250-SIZE 3..."
"....etc..."

but got only
"250-smtp.gmail.com at your servi.."

Here is my codes:

async function send_response(conn, command) {
   await sendCommand(conn, command)
   const reader = conn.readable.getReader();
   
   const { value, done } = await reader.read();
   const response = decoder.decode(value, { stream: true });
   reader.releaseLock();
   return response;
}

async function sendCommand(conn, command) {
   await conn.write(encoder.encode(command))
}

and

const conn = await Deno.connect({ hostname: SMTP_SERVER, port: SMTP_PORT, tcp: 'tcp' });
const response = await send_response(conn, "EHLO mail.example.com\r\n");

Appreciate for any suggestion;

@marvinhagemeister
Copy link
Contributor

FYI: I've edited the issue description to properly format the code snippets to make it easier to read. Using single backticks doesn't work well with code multiline code snippets. These are typically formatted with three backticks and a language specifier so that you get nice syntax highlighting. Example:

```ts
console.log("hey")
```

...looks like this:

console.log("hey")

You can learn more about how to format code on GitHub here https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

@WWRS
Copy link

WWRS commented Mar 19, 2025

With

const encoder = new TextEncoder();
const decoder = new TextDecoder();

// your code

const conn = await Deno.connect({ hostname: 'smtp.gmail.com', port: 25, tcp: 'tcp' });
const response = await send_response(conn, "EHLO mail.example.com\r\n");
console.log(response);
const response2 = await send_response(conn, "EHLO mail.example.com\r\n");
console.log(response2);

I am receiving

220 smtp.gmail.com ESMTP ... - gsmtp

250-smtp.gmail.com at your service, [...]
250-SIZE 3...
...

More details may be necessary to replicate your issue. Is this the same server and port you're using? And what source are you getting the expected output from? Do you get the expected value when you connect to the SMTP server using a platform other than Deno?

@Srabutdotcom
Copy link
Author

@WWRS you made twice call and the output are as expected., but it raises the uncertainty, the needed to call it once or twice or even more.
to note, I can't connect to port 25 from my pc, port 587 succeed.
Anyway, thanks for the initial solution.,

@WWRS
Copy link

WWRS commented Mar 20, 2025

Oh, I see the issue. When you connect, the first chunk

220 smtp.gmail.com ESMTP ... - gsmtp

is loaded into the readable stream of conn. When you EHLO, the second chunk

250-smtp.gmail.com at your service, [...]
250-SIZE 3...
...

is loaded. If you haven't consumed the first chunk, it gets printed the first time you call send_response. The EHLO chunk is still in the readable stream at this point, you just haven't accessed it. If you were to, say, send_response(conn, "QUIT") at this time, the EHLO chunk would print and the QUIT chunk would still be in the stream.

const conn = await Deno.connect({ hostname: 'smtp.gmail.com', port: 587, tcp: 'tcp' });
const reader = conn.readable.getReader();
await reader.read();  // Consume the 220 chunk
reader.releaseLock();

const response = await send_response(conn, "EHLO mail.example.com\r\n");
console.log(response);

will drop the first chunk and synchronize send_response.

@Srabutdotcom
Copy link
Author

Srabutdotcom commented Mar 20, 2025

I got the point now,

When connecting to smtp server the first thing to do is read the response instead of send "EHLO..." message;
So, my code was written in the wrong order making the first chunk unread in the stream.

However, it should be noted that one on one message should be complied when making peer to peer communication. Otherwise, we should make a loop that will make the process take a longer time.

Thanks anyway.,
this case can be closed.

@iuioiua
Copy link
Contributor

iuioiua commented Mar 21, 2025

@Srabutdotcom You can close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants