From 9d3e8b47e7390a7316f3bac1c4bc6619bcecb387 Mon Sep 17 00:00:00 2001 From: bogeychan Date: Wed, 22 May 2024 18:25:03 +0200 Subject: [PATCH 1/2] fix: add `aot` option --- src/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0e70509..6208dbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,6 +72,12 @@ export interface JWTOption< * @see [RFC7519#section-4.1.4](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4) */ exp?: string | number + /** + * Disable Ahead of Time compliation + * + * Reduced performance but faster startup time + */ + aot?: boolean } export const jwt = < @@ -88,6 +94,7 @@ export const jwt = < // Start JWT Payload nbf, exp, + aot = true, ...payload }: // End JWT Payload JWTOption) => { @@ -96,6 +103,8 @@ JWTOption) => { const key = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret + const dynamic = !aot + const validator = schema ? getSchemaValidator( t.Intersect([ @@ -112,7 +121,7 @@ JWTOption) => { iat: t.Optional(t.String()) }) ]), - {} + { dynamic } ) : undefined From 8fe362dcd6ef9b35c1fcef4330e634b21b752282 Mon Sep 17 00:00:00 2001 From: bogeychan Date: Wed, 22 May 2024 18:31:50 +0200 Subject: [PATCH 2/2] chore: add test for aot --- test/index.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index 12faa59..ffa0959 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -43,4 +43,30 @@ describe('Static Plugin', () => { expect(name).toBe(signed.name) }) + + it('no aot', async () => { + + const native = globalThis.Function + + let fnUsed = false + + // @ts-ignore + globalThis.Function = () => { + fnUsed = true + return () => {} + } + + jwt({ + name: 'jwt', + secret: 'A', + aot: false, + schema: t.Object({ + id: t.Number() + }) + }) + + globalThis.Function = native + + expect(fnUsed).toBeFalse() + }) })