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 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() + }) })