EC-Nordbund / denomailer

A SMTP-Client implementation for deno (to send mails!)
https://deno.land/x/denomailer
MIT License
52 stars 17 forks source link

[Regression] Supabase edge functions v1.4.0 works, v1.5.0 and later doesn't #82

Open mjp-free-tech opened 1 year ago

mjp-free-tech commented 1 year ago

I have write some code to send emails using supabase edge function. Using library of 1.4.0 all works good, but 1.5.0 and 1.6.0 doesn't works

Code:

import { SMTPClient } from 'https://deno.land/x/denomailer@1.4.0/mod.ts'
import { decode } from "https://deno.land/x/djwt@v3.0.1/mod.ts";
import { corsHeaders } from "../_shared/cors.ts";

const MIN_ACCESS_LEVEL = 2;
const SMTP_FROM = Deno.env.get('SMTP_FROM');
const options = {
  connection: {
    hostname: Deno.env.get('SMTP_HOST'),
    port: Number(Deno.env.get('SMTP_PORT')),
    tls: false,
    auth: {
      username: Deno.env.get('SMTP_USER'),
      password:  Deno.env.get('SMTP_PASS'),
    },
  },
  debug: {
    log: true,
    allowUnsecure: true,
    encodeLB: false,
    noStartTLS: false,
  },
};

Deno.serve(async (req) => {
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }

  const jwt = req.headers.get("Authorization")?.split(' ')[1];
  const [_header, payload, _signature] = decode(jwt);

  const accessLevel = payload?.app_metadata?.appClaims?.accessLevel;

  if (typeof accessLevel !== 'number' || accessLevel < MIN_ACCESS_LEVEL) {
    return new Response(null, { status: 403 });
  }

  const requestBody = await req.json();
  const { to, subject, content } = requestBody;
  const from = SMTP_FROM;
  const message = { from, to, subject, content };

  const smtp = new SMTPClient(options);
  await smtp.send(message);

  const data = { status: "ok" };

  return new Response(
    JSON.stringify(data),
    { headers: { ...corsHeaders, "Content-Type": "application/json" } },
  );
});

Error stack trace:

BadResource: TCP stream is currently in use
    at opStartTls (ext:deno_net/02_tls.js:10:15)
    at Object.startTls (ext:deno_net/02_tls.js:88:57)
    at #prepareConnection (https://deno.land/x/denomailer@1.5.0/client/basic/client.ts:196:37)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async https://deno.land/x/denomailer@1.5.0/client/basic/client.ts:35:13
    at async SMTPClient.send (https://deno.land/x/denomailer@1.5.0/client/basic/client.ts:49:9)
tobico commented 1 year ago

I'm seeing the same error using denomailer in a Supabase edge function. Downgrading to 1.4.0 resolves the issue.