elysiajs / eden

Fully type-safe Elysia client
MIT License
169 stars 40 forks source link

Error: Body Already Used in github function CD #125

Open lomithrani opened 2 months ago

lomithrani commented 2 months ago

I have trouble in my CD since last update (0.7.2 to 1.1.2) , bun test works fine locally but wont pass in github. I have another old branch on 0.7.16 that doesn't have this issue either. Link to action :

Run cd server
  cd server
  bun install
  bun test
  shell: /usr/bin/bash -e {0}
  env:
    PORT: 3000
    MONGO_URL: mongodb://localhost:3[2](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:2)768/mytestdb
    JWT_SECRET: JWT_SECRET
    ALLOWED_DOMAINS: ["http://localhost:417[3](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:3)"]
bun install v1.1.26 (0a37[4](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:4)23b)

+ typescript@[5](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:5).5.4
+ @elysiajs/eden@1.1.2
+ bun-types@1.1.24
+ @elysiajs/cors@1.1.0
+ @elysiajs/jwt@1.1.0
+ @elysiajs/swagger@1.0.5
+ mongoose@8.5.3
+ portfolio-common@workspace:common

3[6](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:6)2 packages installed [954.00ms]

Blocked 1 postinstall. Run `bun pm untrusted` for details.
bun test v1.1.26 (0a3[7](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:7)423b)

tests/auth.test.ts:
  Running at http://:::3000 CORS allowed: "[\"http://localhost:4173\"]"
  (pass) login > Should create new user [150.26ms]
  (pass) login > Should return existing user [3.49ms]

tests/eden.test.ts:
  Service is healthy!
  (pass) edenTreaty > edenTreaty should have correct methods [1.1[8](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:8)ms]

  Error: 
        at /home/runner/work/portfolio/portfolio/node_modules/@elysiajs/eden/dist/index.js:1:[9](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:9)150
  1 | "use strict";var P=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var re=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var ae=(e,t)=>{for(var n in t)P(e,n,{get:t[n],enumerable:!0})},ie=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of re(t))!se.call(e,s)&&s!==n&&P(e,s,{get:()=>t[s],enumerable:!(r=ne(t,s))||r.enumerable});return e};var oe=e=>ie(P({},"__esModule",{value:!0}),e);var be={};ae(be,{edenFetch:()=>te,edenTreaty:()=>ee,treaty:()=>z});module.exports=oe(be);var T=class extends Error{constructor(n,r){super(r+"");this.status=n;this.value=r}};var ce=/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/,fe=/(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{2}\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT(?:\+|-)\d{4}\s\([^)]+\)/,de=/^(?:(?:(?:(?:0?[1-9]|[[12](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:13)][0-9]|3[01])[/\s-](

  error: Body already used
   code: "ERR_BODY_ALREADY_USED"

        at /home/runner/work/portfolio/portfolio/node_modules/@elysiajs/eden/dist/index.js:1:9150
  (fail) edenTreaty > edentTreaty should return correct responses [1.12ms]

 3 pass
 1 fail
 [14](https://github.com/lomithrani/portfolio/actions/runs/10538541982/job/29201159241?pr=56#step:5:15) expect() calls
Ran 4 tests across 2 files. [675.00ms]

Actual test :

import { test, expect, describe, beforeAll, afterAll } from 'bun:test'
import { edenTreaty } from '@elysiajs/eden'
import type { Portfolio } from '..'
import { ErrorLike, Subprocess, fetch } from 'bun';

let appInstance: Subprocess<"ignore", "inherit", "inherit"> | undefined = undefined;

const api = edenTreaty<Portfolio>("http://localhost:3000");

describe("edenTreaty", () => {
  beforeAll(async () => {
    appInstance = Bun.spawn({
      cmd: ['bun', 'run', 'index.ts'],
      env: process.env,
      stdout: 'inherit',
      stderr: 'inherit',
    });
    await waitForHealthCheck()
  });

  afterAll(() => {
    if (appInstance) {
      appInstance.kill();
    }
  });

  test("edenTreaty should have correct methods", async () => {
    expect(api).toBeDefined()
    expect(api.login.post).toBeDefined()
    expect(api.domain[':name'].get).toBeDefined()
  })

  test("edentTreaty should return correct responses", async () => {
    const mySelfDomainResponse = await api.domain['louis.gentil'].get()
    expect(mySelfDomainResponse).toBeDefined()
    expect(mySelfDomainResponse.status).toBe(200)
    expect(mySelfDomainResponse.data).toBeDefined()
    expect(mySelfDomainResponse.data).toBeObject()
  })

})

async function waitForHealthCheck(url = "http://localhost:3000/health", maxAttempts = 10, interval = 1000) {
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      const response = await fetch(url);

      if (response.status === 200) {
        console.log("Service is healthy!");
        return true;
      } else {
        console.log(`Attempt ${attempt}: Received status ${response.status}. Retrying in ${interval / 1000} seconds...`);
      }
    } catch (error) {
      console.log(`Attempt ${attempt}: Error occurred - ${error}. Retrying in ${interval / 1000} seconds...`);
    }

    if (attempt < maxAttempts) {
      await new Promise(resolve => setTimeout(resolve, interval));
    }
  }

  console.log("Failed to receive a 200 status after max attempts.");
  return false;
}
kravetsone commented 2 months ago

Yeah! I receive it in tests with eden too