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

Unusual crash on every 2nd invocation when using t.Intersect and t.RegExp global validation #121

Open
EvHaus opened this issue Aug 5, 2024 · 0 comments

Comments

@EvHaus
Copy link

EvHaus commented Aug 5, 2024

I came across a really strange bug. It took me a long time to narrow it down to a minimal repro, so please let me know if anything below is unclear.

Using:

  • elysia v1.1.5
  • @elysiajs/eden v1.1.2

The Bug

Using treaty on a very simple endpoint works, but only for the first call. Every second call to this endpoint will fail (even if all incoming parameters & conditions are the same).

const api = treaty(app);

await api.foo.patch({ num: '5' }); // This works
await api.foo.patch({ num: '5' }); // This fails
await api.foo.patch({ num: '5' }); // This works
await api.foo.patch({ num: '5' }); // This fails
// etc...

The failure

For the invocations which fail, the stack trace is:

1 | var s=class extends Error{constructor(e,n){super(n+"");this.status=e;this.value=n}};var i=/(\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))/,o=/(?: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\([^)]+\)/,c=/^(?:(?:(?:(?:0?[1-9]|[12][0-9]|3[01])[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:19|20)\d{2})|(?:(?:19|20)\d{2}[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:0?[1-9]|[12][0-9]|3[01]))))(?:\s(?:1[012]|0?[1-9]):[0-5][0-9](?::[0-5][0-9])?(?:\s[AP]M)?)?$/,u=t=>t.trim().length!==0&&!Number.isNaN(Number(t)),d=t=>{if(typeof t!="string")return null;let r=t.replace(/"/g,"");if(i.test(r)||o.test(r)||c.test(r)){let e=new Date(r);if(!Number.isNaN(e.getTime()))return e}return null},a=t=>{let r=t.charCodeAt(0),e=t.charCodeAt(t.length-1);return r===123&&e===125||r===91&&e===93},p=t=>JSON.parse(t,(r,e)=>{ | ... truncated

error: [object Object]
      at new s (/myproject/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected]_/node_modules/@elysiajs/eden/dist/chunk-XYW4OUFN.mjs:1:44)
      at /myproject/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected]_/node_modules/@elysiajs/eden/dist/chunk-7WO4HTSU.mjs:1:4302

Conditions

To reproduce this bug you'll need to create an endpoint with the following body schema validation:

import { Elysia, t } from 'elysia';

const app = new Elysia().patch(
	'/foo',
	() => ({ done: true }),
	{
		body: t.Intersect([
			t.Object({
				num: t.Optional(
					// Any digit 
					t.RegExp(/\d/g),
				),
			}),
			t.Object({}),
		]),
	},
);

export default app;

This bug only happens if using t.Intersect() and t.Optional() and t.RegExp() (any regex pattern will do). Removing any of these elements, makes the issue go away. Also interestingly, removing the g flag on the RegExp pattern also makes the issue go away.

Because of this behaviour, I can't quite tell if the issue is in elysia or @elysiajs/eden or typebox. Based on the stacktrace, I'm thinking Eden, but I'm not 100% sure.

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

1 participant