@clerk/fastify 3.1.27-snapshot.v20260515233846 → 3.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,7 @@
1
1
  import '@clerk/shared/types';
2
- import { ClerkClient, ClerkOptions } from '@clerk/backend';
2
+ import { ClerkOptions } from '@clerk/backend';
3
3
  import { ShouldProxyFn } from '@clerk/shared/proxy';
4
4
 
5
- declare module 'fastify' {
6
- interface FastifyRequest {
7
- clerk: ClerkClient;
8
- }
9
- }
10
5
  declare const ALLOWED_HOOKS: readonly ["onRequest", "preHandler"];
11
6
  /**
12
7
  * Options for the built-in Frontend API proxy.
@@ -25,14 +20,6 @@ interface FrontendApiProxyOptions {
25
20
  type ClerkFastifyOptions = ClerkOptions & {
26
21
  hookName?: (typeof ALLOWED_HOOKS)[number];
27
22
  frontendApiProxy?: FrontendApiProxyOptions;
28
- /**
29
- * Whether to enable the handshake flow for session verification.
30
- * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
31
- * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
32
- * from blocking authentication on subsequent API requests.
33
- * @default true
34
- */
35
- enableHandshake?: boolean;
36
23
  };
37
24
 
38
25
  export type { ClerkFastifyOptions as C, FrontendApiProxyOptions as F };
@@ -1,12 +1,7 @@
1
1
  import '@clerk/shared/types';
2
- import { ClerkClient, ClerkOptions } from '@clerk/backend';
2
+ import { ClerkOptions } from '@clerk/backend';
3
3
  import { ShouldProxyFn } from '@clerk/shared/proxy';
4
4
 
5
- declare module 'fastify' {
6
- interface FastifyRequest {
7
- clerk: ClerkClient;
8
- }
9
- }
10
5
  declare const ALLOWED_HOOKS: readonly ["onRequest", "preHandler"];
11
6
  /**
12
7
  * Options for the built-in Frontend API proxy.
@@ -25,14 +20,6 @@ interface FrontendApiProxyOptions {
25
20
  type ClerkFastifyOptions = ClerkOptions & {
26
21
  hookName?: (typeof ALLOWED_HOOKS)[number];
27
22
  frontendApiProxy?: FrontendApiProxyOptions;
28
- /**
29
- * Whether to enable the handshake flow for session verification.
30
- * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling
31
- * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks
32
- * from blocking authentication on subsequent API requests.
33
- * @default true
34
- */
35
- enableHandshake?: boolean;
36
23
  };
37
24
 
38
25
  export type { ClerkFastifyOptions as C, FrontendApiProxyOptions as F };
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _clerk_backend from '@clerk/backend';
2
2
  export * from '@clerk/backend';
3
- import { C as ClerkFastifyOptions } from './index-C5pGnL7a.mjs';
4
- export { F as FrontendApiProxyOptions } from './index-C5pGnL7a.mjs';
3
+ import { C as ClerkFastifyOptions } from './index-CWnSaB0B.mjs';
4
+ export { F as FrontendApiProxyOptions } from './index-CWnSaB0B.mjs';
5
5
  import { FastifyPluginCallback, FastifyRequest } from 'fastify';
6
6
  import { GetAuthFn } from '@clerk/backend/internal';
7
7
  import '@clerk/shared/types';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _clerk_backend from '@clerk/backend';
2
2
  export * from '@clerk/backend';
3
- import { C as ClerkFastifyOptions } from './index-C5pGnL7a.js';
4
- export { F as FrontendApiProxyOptions } from './index-C5pGnL7a.js';
3
+ import { C as ClerkFastifyOptions } from './index-CWnSaB0B.js';
4
+ export { F as FrontendApiProxyOptions } from './index-CWnSaB0B.js';
5
5
  import { FastifyPluginCallback, FastifyRequest } from 'fastify';
6
6
  import { GetAuthFn } from '@clerk/backend/internal';
7
7
  import '@clerk/shared/types';
package/dist/index.js CHANGED
@@ -45,11 +45,13 @@ var import_fastify_plugin = __toESM(require("fastify-plugin"));
45
45
  var ALLOWED_HOOKS = ["onRequest", "preHandler"];
46
46
 
47
47
  // src/withClerkMiddleware.ts
48
- var import_backend = require("@clerk/backend");
49
48
  var import_internal2 = require("@clerk/backend/internal");
50
49
  var import_proxy = require("@clerk/backend/proxy");
51
50
  var import_stream2 = require("stream");
52
51
 
52
+ // src/clerkClient.ts
53
+ var import_backend = require("@clerk/backend");
54
+
53
55
  // src/constants.ts
54
56
  var import_internal = require("@clerk/backend/internal");
55
57
  var import_apiUrlFromPublishableKey = require("@clerk/shared/apiUrlFromPublishableKey");
@@ -61,11 +63,22 @@ var API_URL = process.env.CLERK_API_URL || (0, import_apiUrlFromPublishableKey.a
61
63
  var JWT_KEY = process.env.CLERK_JWT_KEY || "";
62
64
  var SDK_METADATA = {
63
65
  name: "@clerk/fastify",
64
- version: "3.1.27-snapshot.v20260515233846",
66
+ version: "3.1.27",
65
67
  environment: "production"
66
68
  };
67
69
  var { Cookies, Headers: Headers2 } = import_internal.constants;
68
70
 
71
+ // src/clerkClient.ts
72
+ var clerkClient = (0, import_backend.createClerkClient)({
73
+ secretKey: SECRET_KEY,
74
+ machineSecretKey: MACHINE_SECRET_KEY,
75
+ apiUrl: API_URL,
76
+ apiVersion: API_VERSION,
77
+ jwtKey: JWT_KEY,
78
+ userAgent: `${"@clerk/fastify"}@${"3.1.27"}`,
79
+ sdkMetadata: SDK_METADATA
80
+ });
81
+
69
82
  // src/utils.ts
70
83
  var import_stream = require("stream");
71
84
  var fastifyRequestToRequest = (req) => {
@@ -118,21 +131,9 @@ var requestToProxyRequest = (req) => {
118
131
  var withClerkMiddleware = (options) => {
119
132
  const frontendApiProxy = options.frontendApiProxy;
120
133
  const proxyPath = (0, import_proxy.stripTrailingSlashes)(frontendApiProxy?.path ?? import_proxy.DEFAULT_PROXY_PATH) || import_proxy.DEFAULT_PROXY_PATH;
121
- const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
122
- const secretKey = options.secretKey || SECRET_KEY;
123
- const clerkClient2 = (0, import_backend.createClerkClient)({
124
- ...options,
125
- publishableKey,
126
- secretKey,
127
- machineSecretKey: options.machineSecretKey || MACHINE_SECRET_KEY,
128
- apiUrl: options.apiUrl || API_URL,
129
- apiVersion: options.apiVersion || API_VERSION,
130
- jwtKey: options.jwtKey || JWT_KEY,
131
- userAgent: options.userAgent || `${SDK_METADATA.name}@${SDK_METADATA.version}`,
132
- sdkMetadata: options.sdkMetadata || SDK_METADATA
133
- });
134
- const enableHandshake = options.enableHandshake ?? true;
135
134
  return async (fastifyRequest, reply) => {
135
+ const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
136
+ const secretKey = options.secretKey || SECRET_KEY;
136
137
  let resolvedProxyUrl = options.proxyUrl;
137
138
  if (frontendApiProxy) {
138
139
  const requestUrl = new URL(
@@ -178,7 +179,7 @@ var withClerkMiddleware = (options) => {
178
179
  }
179
180
  }
180
181
  const req = fastifyRequestToRequest(fastifyRequest);
181
- const requestState = await clerkClient2.authenticateRequest(req, {
182
+ const requestState = await clerkClient.authenticateRequest(req, {
182
183
  ...options,
183
184
  secretKey,
184
185
  publishableKey,
@@ -186,23 +187,19 @@ var withClerkMiddleware = (options) => {
186
187
  acceptsToken: "any"
187
188
  });
188
189
  requestState.headers.forEach((value, key) => reply.header(key, value));
189
- if (enableHandshake) {
190
- const locationHeader = requestState.headers.get(Headers2.Location);
191
- if (locationHeader) {
192
- return reply.code(307).send();
193
- } else if (requestState.status === import_internal2.AuthStatus.Handshake) {
194
- throw new Error("Clerk: handshake status without redirect");
195
- }
190
+ const locationHeader = requestState.headers.get(Headers2.Location);
191
+ if (locationHeader) {
192
+ return reply.code(307).send();
193
+ } else if (requestState.status === import_internal2.AuthStatus.Handshake) {
194
+ throw new Error("Clerk: handshake status without redirect");
196
195
  }
197
196
  fastifyRequest.auth = requestState.toAuth();
198
- fastifyRequest.clerk = clerkClient2;
199
197
  };
200
198
  };
201
199
 
202
200
  // src/clerkPlugin.ts
203
201
  var plugin = (instance, opts, done) => {
204
202
  instance.decorateRequest("auth", null);
205
- instance.decorateRequest("clerk", null);
206
203
  const hookName = opts.hookName || "preHandler";
207
204
  if (!ALLOWED_HOOKS.includes(hookName)) {
208
205
  throw new Error(`Unsupported hookName: ${hookName}`);
@@ -243,18 +240,6 @@ var getAuth = ((req, options) => {
243
240
  }
244
241
  return (0, import_internal3.getAuthObjectForAcceptedToken)({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });
245
242
  });
246
-
247
- // src/clerkClient.ts
248
- var import_backend2 = require("@clerk/backend");
249
- var clerkClient = (0, import_backend2.createClerkClient)({
250
- secretKey: SECRET_KEY,
251
- machineSecretKey: MACHINE_SECRET_KEY,
252
- apiUrl: API_URL,
253
- apiVersion: API_VERSION,
254
- jwtKey: JWT_KEY,
255
- userAgent: `${"@clerk/fastify"}@${"3.1.27-snapshot.v20260515233846"}`,
256
- sdkMetadata: SDK_METADATA
257
- });
258
243
  // Annotate the CommonJS export names for ESM import in node:
259
244
  0 && (module.exports = {
260
245
  clerkClient,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/constants.ts","../src/utils.ts","../src/getAuth.ts","../src/errors.ts","../src/clerkClient.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n instance.decorateRequest('clerk', null as any);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkClient, ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n clerk: ClerkClient;\n }\n}\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n /**\n * Whether to enable the handshake flow for session verification.\n * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling\n * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks\n * from blocking authentication on subsequent API requests.\n * @default true\n */\n enableHandshake?: boolean;\n};\n","import { createClerkClient } from '@clerk/backend';\nimport { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n const clerkClient = createClerkClient({\n ...options,\n publishableKey,\n secretKey,\n machineSecretKey: options.machineSecretKey || constants.MACHINE_SECRET_KEY,\n apiUrl: options.apiUrl || constants.API_URL,\n apiVersion: options.apiVersion || constants.API_VERSION,\n jwtKey: options.jwtKey || constants.JWT_KEY,\n userAgent: options.userAgent || `${constants.SDK_METADATA.name}@${constants.SDK_METADATA.version}`,\n sdkMetadata: options.sdkMetadata || constants.SDK_METADATA,\n });\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n if (enableHandshake) {\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n fastifyRequest.clerk = clerkClient;\n };\n};\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nexport const fastifyRequestToRequest = (req: FastifyRequest): Request => {\n const headers = new Headers(\n Object.keys(req.headers).reduce((acc, key) => {\n const value = req.headers[key];\n if (!value) {\n return acc;\n }\n\n if (typeof value === 'string') {\n acc.set(key, value);\n } else {\n acc.set(key, value.join(','));\n }\n return acc;\n }, new Headers()),\n );\n\n // Making some manual tests it seems that FastifyRequest populates the req protocol / hostname\n // based on the forwarded headers. Nevertheless, we are gonna use a dummy base and the request\n // will be fixed by the internals of the clerk/backend package\n const dummyOriginReqUrl = new URL(req.url || '', `${req.protocol}://clerk-dummy`);\n return new Request(dummyOriginReqUrl, {\n method: req.method,\n headers,\n });\n};\n\n/**\n * Converts a Fastify request to a Fetch API Request with full headers and body streaming,\n * suitable for proxy forwarding.\n */\nexport const requestToProxyRequest = (req: FastifyRequest): Request => {\n const headers = new Headers();\n Object.entries(req.headers).forEach(([key, value]) => {\n if (value) {\n headers.set(key, Array.isArray(value) ? value.join(', ') : value);\n }\n });\n\n const forwardedProto = req.headers['x-forwarded-proto'];\n const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;\n const proto = (protoHeader || '').split(',')[0].trim();\n const protocol = proto === 'https' || req.protocol === 'https' ? 'https' : 'http';\n\n const forwardedHost = req.headers['x-forwarded-host'];\n const hostHeader = Array.isArray(forwardedHost) ? forwardedHost[0] : forwardedHost;\n const host = (hostHeader || '').split(',')[0].trim() || req.hostname || 'localhost';\n\n const url = new URL(req.url || '', `${protocol}://${host}`);\n\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method);\n\n return new Request(url.toString(), {\n method: req.method,\n headers,\n body: hasBody ? (Readable.toWeb(req.raw) as ReadableStream) : undefined,\n // @ts-expect-error - duplex required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n });\n};\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,2BAAd;;;ACCA,4BAAe;;;ACQR,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACTvD,qBAAkC;AAClC,IAAAA,mBAA2B;AAC3B,mBAAgF;AAEhF,IAAAC,iBAAyB;;;ACJzB,sBAA0B;AAC1B,sCAAyC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,QAAQ,IAAI,yBAAyB;AAC7D,IAAM,UAAU,QAAQ,IAAI,qBAAiB,0DAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,iBAAiB;AAC7C,IAAM,eAAe;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,aAAa;AACf;AAEO,IAAM,EAAE,SAAS,SAAAC,SAA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ACR,WAAW,GAAG,gBAAY,IAAI,iCAAe;AAAA,EAC7C,aAAa;AACf,CAAC;","names":["import_internal","import_stream","Headers","clerkClient","Headers","fp","import_internal","import_backend"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/utils.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nexport const fastifyRequestToRequest = (req: FastifyRequest): Request => {\n const headers = new Headers(\n Object.keys(req.headers).reduce((acc, key) => {\n const value = req.headers[key];\n if (!value) {\n return acc;\n }\n\n if (typeof value === 'string') {\n acc.set(key, value);\n } else {\n acc.set(key, value.join(','));\n }\n return acc;\n }, new Headers()),\n );\n\n // Making some manual tests it seems that FastifyRequest populates the req protocol / hostname\n // based on the forwarded headers. Nevertheless, we are gonna use a dummy base and the request\n // will be fixed by the internals of the clerk/backend package\n const dummyOriginReqUrl = new URL(req.url || '', `${req.protocol}://clerk-dummy`);\n return new Request(dummyOriginReqUrl, {\n method: req.method,\n headers,\n });\n};\n\n/**\n * Converts a Fastify request to a Fetch API Request with full headers and body streaming,\n * suitable for proxy forwarding.\n */\nexport const requestToProxyRequest = (req: FastifyRequest): Request => {\n const headers = new Headers();\n Object.entries(req.headers).forEach(([key, value]) => {\n if (value) {\n headers.set(key, Array.isArray(value) ? value.join(', ') : value);\n }\n });\n\n const forwardedProto = req.headers['x-forwarded-proto'];\n const protoHeader = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto;\n const proto = (protoHeader || '').split(',')[0].trim();\n const protocol = proto === 'https' || req.protocol === 'https' ? 'https' : 'http';\n\n const forwardedHost = req.headers['x-forwarded-host'];\n const hostHeader = Array.isArray(forwardedHost) ? forwardedHost[0] : forwardedHost;\n const host = (hostHeader || '').split(',')[0].trim() || req.hostname || 'localhost';\n\n const url = new URL(req.url || '', `${protocol}://${host}`);\n\n const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method);\n\n return new Request(url.toString(), {\n method: req.method,\n headers,\n body: hasBody ? (Readable.toWeb(req.raw) as ReadableStream) : undefined,\n // @ts-expect-error - duplex required for streaming bodies but not in all TS definitions\n duplex: hasBody ? 'half' : undefined,\n });\n};\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,2BAAd;;;ACCA,4BAAe;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,IAAAA,mBAA2B;AAC3B,mBAAgF;AAEhF,IAAAC,iBAAyB;;;ACHzB,qBAAkC;;;ACAlC,sBAA0B;AAC1B,sCAAyC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,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,KAAK,KAAK,CAAC;AAErE,UAAM,iBAAiB,aAAa,QAAQ,IAAcC,SAAQ,QAAQ;AAC1E,QAAI,gBAAgB;AAClB,aAAO,MAAM,KAAK,GAAG,EAAE,KAAK;AAAA,IAC9B,WAAW,aAAa,WAAW,4BAAW,WAAW;AACvD,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AAGA,mBAAe,OAAO,aAAa,OAAO;AAAA,EAC5C;AACF;;;AFzFA,IAAM,SAAqD,CACzD,UACA,MACA,SACG;AACH,WAAS,gBAAgB,QAAQ,IAAI;AAErC,QAAM,WAAW,KAAK,YAAY;AAClC,MAAI,CAAC,cAAc,SAAS,QAAQ,GAAG;AACrC,UAAM,IAAI,MAAM,yBAAyB,QAAQ,EAAE;AAAA,EACrD;AAEA,WAAS,QAAQ,UAAU,oBAAoB,IAAI,CAAC;AAEpD,OAAK;AACP;AAEO,IAAM,kBAAc,sBAAAC,SAAG,QAAQ;AAAA,EACpC,MAAM;AAAA,EACN,SAAS;AACX,CAAC;;;AM1BD,IAAAC,mBAA8C;;;ACA9C,IAAM,qBAAqB,CAAC,QAAgB;AAC1C,SAAO,oBAAa,IAAI,KAAK,CAAC;AAAA;AAAA;AAAA;AAAA;AAKhC;AAEO,IAAM,6BACX,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAOpB;;;ADXM,IAAM,WAAsC,CAAC,KAAqB,YAA0B;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,aAAO,gDAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":["import_internal","import_stream","Headers","Headers","fp","import_internal"]}
package/dist/index.mjs CHANGED
@@ -13,11 +13,13 @@ import fp from "fastify-plugin";
13
13
  var ALLOWED_HOOKS = ["onRequest", "preHandler"];
14
14
 
15
15
  // src/withClerkMiddleware.ts
16
- import { createClerkClient } from "@clerk/backend";
17
16
  import { AuthStatus } from "@clerk/backend/internal";
18
17
  import { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from "@clerk/backend/proxy";
19
18
  import { Readable } from "stream";
20
19
 
20
+ // src/clerkClient.ts
21
+ import { createClerkClient } from "@clerk/backend";
22
+
21
23
  // src/constants.ts
22
24
  import { constants } from "@clerk/backend/internal";
23
25
  import { apiUrlFromPublishableKey } from "@clerk/shared/apiUrlFromPublishableKey";
@@ -29,30 +31,29 @@ var API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_
29
31
  var JWT_KEY = process.env.CLERK_JWT_KEY || "";
30
32
  var SDK_METADATA = {
31
33
  name: "@clerk/fastify",
32
- version: "3.1.27-snapshot.v20260515233846",
34
+ version: "3.1.27",
33
35
  environment: "production"
34
36
  };
35
37
  var { Cookies, Headers } = constants;
36
38
 
39
+ // src/clerkClient.ts
40
+ var clerkClient = createClerkClient({
41
+ secretKey: SECRET_KEY,
42
+ machineSecretKey: MACHINE_SECRET_KEY,
43
+ apiUrl: API_URL,
44
+ apiVersion: API_VERSION,
45
+ jwtKey: JWT_KEY,
46
+ userAgent: `${"@clerk/fastify"}@${"3.1.27"}`,
47
+ sdkMetadata: SDK_METADATA
48
+ });
49
+
37
50
  // src/withClerkMiddleware.ts
38
51
  var withClerkMiddleware = (options) => {
39
52
  const frontendApiProxy = options.frontendApiProxy;
40
53
  const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;
41
- const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
42
- const secretKey = options.secretKey || SECRET_KEY;
43
- const clerkClient2 = createClerkClient({
44
- ...options,
45
- publishableKey,
46
- secretKey,
47
- machineSecretKey: options.machineSecretKey || MACHINE_SECRET_KEY,
48
- apiUrl: options.apiUrl || API_URL,
49
- apiVersion: options.apiVersion || API_VERSION,
50
- jwtKey: options.jwtKey || JWT_KEY,
51
- userAgent: options.userAgent || `${SDK_METADATA.name}@${SDK_METADATA.version}`,
52
- sdkMetadata: options.sdkMetadata || SDK_METADATA
53
- });
54
- const enableHandshake = options.enableHandshake ?? true;
55
54
  return async (fastifyRequest, reply) => {
55
+ const publishableKey = options.publishableKey || PUBLISHABLE_KEY;
56
+ const secretKey = options.secretKey || SECRET_KEY;
56
57
  let resolvedProxyUrl = options.proxyUrl;
57
58
  if (frontendApiProxy) {
58
59
  const requestUrl = new URL(
@@ -98,7 +99,7 @@ var withClerkMiddleware = (options) => {
98
99
  }
99
100
  }
100
101
  const req = fastifyRequestToRequest(fastifyRequest);
101
- const requestState = await clerkClient2.authenticateRequest(req, {
102
+ const requestState = await clerkClient.authenticateRequest(req, {
102
103
  ...options,
103
104
  secretKey,
104
105
  publishableKey,
@@ -106,23 +107,19 @@ var withClerkMiddleware = (options) => {
106
107
  acceptsToken: "any"
107
108
  });
108
109
  requestState.headers.forEach((value, key) => reply.header(key, value));
109
- if (enableHandshake) {
110
- const locationHeader = requestState.headers.get(Headers.Location);
111
- if (locationHeader) {
112
- return reply.code(307).send();
113
- } else if (requestState.status === AuthStatus.Handshake) {
114
- throw new Error("Clerk: handshake status without redirect");
115
- }
110
+ const locationHeader = requestState.headers.get(Headers.Location);
111
+ if (locationHeader) {
112
+ return reply.code(307).send();
113
+ } else if (requestState.status === AuthStatus.Handshake) {
114
+ throw new Error("Clerk: handshake status without redirect");
116
115
  }
117
116
  fastifyRequest.auth = requestState.toAuth();
118
- fastifyRequest.clerk = clerkClient2;
119
117
  };
120
118
  };
121
119
 
122
120
  // src/clerkPlugin.ts
123
121
  var plugin = (instance, opts, done) => {
124
122
  instance.decorateRequest("auth", null);
125
- instance.decorateRequest("clerk", null);
126
123
  const hookName = opts.hookName || "preHandler";
127
124
  if (!ALLOWED_HOOKS.includes(hookName)) {
128
125
  throw new Error(`Unsupported hookName: ${hookName}`);
@@ -163,18 +160,6 @@ var getAuth = ((req, options) => {
163
160
  }
164
161
  return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });
165
162
  });
166
-
167
- // src/clerkClient.ts
168
- import { createClerkClient as createClerkClient2 } from "@clerk/backend";
169
- var clerkClient = createClerkClient2({
170
- secretKey: SECRET_KEY,
171
- machineSecretKey: MACHINE_SECRET_KEY,
172
- apiUrl: API_URL,
173
- apiVersion: API_VERSION,
174
- jwtKey: JWT_KEY,
175
- userAgent: `${"@clerk/fastify"}@${"3.1.27-snapshot.v20260515233846"}`,
176
- sdkMetadata: SDK_METADATA
177
- });
178
163
  export {
179
164
  clerkClient,
180
165
  clerkPlugin,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/constants.ts","../src/getAuth.ts","../src/errors.ts","../src/clerkClient.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n instance.decorateRequest('clerk', null as any);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkClient, ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n clerk: ClerkClient;\n }\n}\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n /**\n * Whether to enable the handshake flow for session verification.\n * Disable this when using Clerk with a first-party API backend (e.g. a SPA calling\n * a Fastify server) to prevent handshake nonce cookies set during OAuth callbacks\n * from blocking authentication on subsequent API requests.\n * @default true\n */\n enableHandshake?: boolean;\n};\n","import { createClerkClient } from '@clerk/backend';\nimport { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n const clerkClient = createClerkClient({\n ...options,\n publishableKey,\n secretKey,\n machineSecretKey: options.machineSecretKey || constants.MACHINE_SECRET_KEY,\n apiUrl: options.apiUrl || constants.API_URL,\n apiVersion: options.apiVersion || constants.API_VERSION,\n jwtKey: options.jwtKey || constants.JWT_KEY,\n userAgent: options.userAgent || `${constants.SDK_METADATA.name}@${constants.SDK_METADATA.version}`,\n sdkMetadata: options.sdkMetadata || constants.SDK_METADATA,\n });\n const enableHandshake = options.enableHandshake ?? true;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n if (enableHandshake) {\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n fastifyRequest.clerk = clerkClient;\n };\n};\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n"],"mappings":";;;;;;AAAA,cAAc;;;ACCd,OAAO,QAAQ;;;ACQR,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACTvD,SAAS,yBAAyB;AAClC,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB,oBAAoB,4BAA4B;AAEhF,SAAS,gBAAgB;;;ACJzB,SAAS,iBAAiB;AAC1B,SAAS,gCAAgC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,QAAQ,IAAI,yBAAyB;AAC7D,IAAM,UAAU,QAAQ,IAAI,iBAAiB,yBAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,iBAAiB;AAC7C,IAAM,eAAe;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,aAAa;AACf;AAEO,IAAM,EAAE,SAAS,QAAQ,IAAI;;;ADL7B,IAAM,sBAAsB,CAAC,YAAiC;AACnE,QAAM,mBAA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,MAAM,yBAAyB,QAAQ,EAAE;AAAA,EACrD;AAEA,WAAS,QAAQ,UAAU,oBAAoB,IAAI,CAAC;AAEpD,OAAK;AACP;AAEO,IAAM,cAAc,GAAG,QAAQ;AAAA,EACpC,MAAM;AAAA,EACN,SAAS;AACX,CAAC;;;AI5BD,SAAS,qCAAqC;;;ACA9C,IAAM,qBAAqB,CAAC,QAAgB;AAC1C,SAAO,oBAAa,IAAI,KAAK,CAAC;AAAA;AAAA;AAAA;AAAA;AAKhC;AAEO,IAAM,6BACX,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAOpB;;;ADXM,IAAM,WAAsC,CAAC,KAAqB,YAA0B;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,SAAO,8BAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;;;AEdA,SAAS,qBAAAC,0BAAyB;AAI3B,IAAM,cAAcC,mBAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW,GAAG,gBAAY,IAAI,iCAAe;AAAA,EAC7C,aAAa;AACf,CAAC;","names":["clerkClient","createClerkClient","createClerkClient"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/clerkPlugin.ts","../src/types.ts","../src/withClerkMiddleware.ts","../src/clerkClient.ts","../src/constants.ts","../src/getAuth.ts","../src/errors.ts"],"sourcesContent":["export * from '@clerk/backend';\n\nexport type { ClerkFastifyOptions, FrontendApiProxyOptions } from './types';\n\nexport { clerkPlugin } from './clerkPlugin';\nexport { getAuth } from './getAuth';\nexport { clerkClient } from './clerkClient';\n","import type { FastifyInstance, FastifyPluginCallback } from 'fastify';\nimport fp from 'fastify-plugin';\n\nimport type { ClerkFastifyOptions } from './types';\nimport { ALLOWED_HOOKS } from './types';\nimport { withClerkMiddleware } from './withClerkMiddleware';\n\nconst plugin: FastifyPluginCallback<ClerkFastifyOptions> = (\n instance: FastifyInstance,\n opts: ClerkFastifyOptions,\n done,\n) => {\n instance.decorateRequest('auth', null);\n // run clerk as a middleware to all scoped routes\n const hookName = opts.hookName || 'preHandler';\n if (!ALLOWED_HOOKS.includes(hookName)) {\n throw new Error(`Unsupported hookName: ${hookName}`);\n }\n\n instance.addHook(hookName, withClerkMiddleware(opts));\n\n done();\n};\n\nexport const clerkPlugin = fp(plugin, {\n name: '@clerk/fastify',\n fastify: '5.x',\n});\n","import type { ClerkOptions } from '@clerk/backend';\nimport type { ShouldProxyFn } from '@clerk/shared/proxy';\n\nexport const ALLOWED_HOOKS = ['onRequest', 'preHandler'] as const;\n\n/**\n * Options for the built-in Frontend API proxy.\n *\n * When enabled, the middleware intercepts requests that match the proxy path\n * (default `/__clerk`) and forwards them to the Clerk Frontend API, allowing\n * the Clerk frontend SDKs to communicate with Clerk without third-party\n * cookie or ad-blocker issues.\n */\nexport interface FrontendApiProxyOptions {\n /** Toggle the proxy on/off, or supply a function that decides per-request. */\n enabled: boolean | ShouldProxyFn;\n /** Custom path prefix for the proxy (default: `/__clerk`). */\n path?: string;\n}\n\nexport type ClerkFastifyOptions = ClerkOptions & {\n hookName?: (typeof ALLOWED_HOOKS)[number];\n frontendApiProxy?: FrontendApiProxyOptions;\n};\n","import { AuthStatus } from '@clerk/backend/internal';\nimport { clerkFrontendApiProxy, DEFAULT_PROXY_PATH, stripTrailingSlashes } from '@clerk/backend/proxy';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { Readable } from 'stream';\n\nimport { clerkClient } from './clerkClient';\nimport * as constants from './constants';\nimport type { ClerkFastifyOptions } from './types';\nimport { fastifyRequestToRequest, requestToProxyRequest } from './utils';\n\nexport const withClerkMiddleware = (options: ClerkFastifyOptions) => {\n const frontendApiProxy = options.frontendApiProxy;\n const proxyPath = stripTrailingSlashes(frontendApiProxy?.path ?? DEFAULT_PROXY_PATH) || DEFAULT_PROXY_PATH;\n\n return async (fastifyRequest: FastifyRequest, reply: FastifyReply) => {\n const publishableKey = options.publishableKey || constants.PUBLISHABLE_KEY;\n const secretKey = options.secretKey || constants.SECRET_KEY;\n\n // Handle Frontend API proxy requests and auto-derive proxyUrl\n let resolvedProxyUrl = options.proxyUrl;\n if (frontendApiProxy) {\n const requestUrl = new URL(\n fastifyRequest.url,\n `${fastifyRequest.protocol}://${fastifyRequest.hostname || 'localhost'}`,\n );\n const isEnabled =\n typeof frontendApiProxy.enabled === 'function'\n ? frontendApiProxy.enabled(requestUrl)\n : frontendApiProxy.enabled;\n\n if (isEnabled) {\n if (requestUrl.pathname === proxyPath || requestUrl.pathname.startsWith(proxyPath + '/')) {\n const proxyRequest = requestToProxyRequest(fastifyRequest);\n\n const proxyResponse = await clerkFrontendApiProxy(proxyRequest, {\n proxyPath,\n publishableKey,\n secretKey,\n });\n\n reply.code(proxyResponse.status);\n proxyResponse.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n\n if (proxyResponse.body) {\n const reader = proxyResponse.body.getReader();\n const stream = new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (error) {\n this.destroy(error instanceof Error ? error : new Error(String(error)));\n }\n },\n });\n return reply.send(stream);\n }\n return reply.send();\n }\n\n // Pass just the path - the backend resolves it against the request's\n // public origin (from x-forwarded-* headers).\n if (!resolvedProxyUrl) {\n resolvedProxyUrl = proxyPath;\n }\n }\n }\n\n const req = fastifyRequestToRequest(fastifyRequest);\n\n const requestState = await clerkClient.authenticateRequest(req, {\n ...options,\n secretKey,\n publishableKey,\n proxyUrl: resolvedProxyUrl,\n acceptsToken: 'any',\n });\n\n requestState.headers.forEach((value, key) => reply.header(key, value));\n\n const locationHeader = requestState.headers.get(constants.Headers.Location);\n if (locationHeader) {\n return reply.code(307).send();\n } else if (requestState.status === AuthStatus.Handshake) {\n throw new Error('Clerk: handshake status without redirect');\n }\n\n // @ts-expect-error Inject auth so getAuth can read it\n fastifyRequest.auth = requestState.toAuth();\n };\n};\n","import { createClerkClient } from '@clerk/backend';\n\nimport { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';\n\nexport const clerkClient = createClerkClient({\n secretKey: SECRET_KEY,\n machineSecretKey: MACHINE_SECRET_KEY,\n apiUrl: API_URL,\n apiVersion: API_VERSION,\n jwtKey: JWT_KEY,\n userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,\n sdkMetadata: SDK_METADATA,\n});\n","import { constants } from '@clerk/backend/internal';\nimport { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey';\n\nexport const API_VERSION = process.env.CLERK_API_VERSION || 'v1';\nexport const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';\nexport const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';\nexport const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';\nexport const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);\nexport const JWT_KEY = process.env.CLERK_JWT_KEY || '';\nexport const SDK_METADATA = {\n name: PACKAGE_NAME,\n version: PACKAGE_VERSION,\n environment: process.env.NODE_ENV,\n};\n\nexport const { Cookies, Headers } = constants;\n","import type { AuthOptions, GetAuthFn, SignedInAuthObject, SignedOutAuthObject } from '@clerk/backend/internal';\nimport { getAuthObjectForAcceptedToken } from '@clerk/backend/internal';\nimport type { FastifyRequest } from 'fastify';\n\nimport { pluginRegistrationRequired } from './errors';\n\nexport const getAuth: GetAuthFn<FastifyRequest> = ((req: FastifyRequest, options?: AuthOptions) => {\n const authReq = req as FastifyRequest & { auth: SignedInAuthObject | SignedOutAuthObject };\n\n if (!authReq.auth) {\n throw new Error(pluginRegistrationRequired);\n }\n\n return getAuthObjectForAcceptedToken({ authObject: authReq.auth, acceptsToken: options?.acceptsToken });\n}) as GetAuthFn<FastifyRequest>;\n","// https://github.com/clerk/javascript/blob/main/packages/remix/src/errors.ts#L1-L0\nconst createErrorMessage = (msg: string) => {\n return `🔒 Clerk: ${msg.trim()}\n\nFor more info, check out the docs: https://clerk.com/docs,\nor come say hi in our discord server: https://clerk.com/discord\n`;\n};\n\nexport const pluginRegistrationRequired =\n createErrorMessage(`The \"clerkPlugin\" should be registered before using the \"getAuth\".\nExample:\n\nimport { clerkPlugin } from '@clerk/fastify';\n\nconst server: FastifyInstance = Fastify({ logger: true });\nserver.register(clerkPlugin);\n`);\n"],"mappings":";;;;;;AAAA,cAAc;;;ACCd,OAAO,QAAQ;;;ACER,IAAM,gBAAgB,CAAC,aAAa,YAAY;;;ACHvD,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB,oBAAoB,4BAA4B;AAEhF,SAAS,gBAAgB;;;ACHzB,SAAS,yBAAyB;;;ACAlC,SAAS,iBAAiB;AAC1B,SAAS,gCAAgC;AAElC,IAAM,cAAc,QAAQ,IAAI,qBAAqB;AACrD,IAAM,aAAa,QAAQ,IAAI,oBAAoB;AACnD,IAAM,qBAAqB,QAAQ,IAAI,4BAA4B;AACnE,IAAM,kBAAkB,QAAQ,IAAI,yBAAyB;AAC7D,IAAM,UAAU,QAAQ,IAAI,iBAAiB,yBAAyB,eAAe;AACrF,IAAM,UAAU,QAAQ,IAAI,iBAAiB;AAC7C,IAAM,eAAe;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,aAAa;AACf;AAEO,IAAM,EAAE,SAAS,QAAQ,IAAI;;;ADX7B,IAAM,cAAc,kBAAkB;AAAA,EAC3C,WAAW;AAAA,EACX,kBAAkB;AAAA,EAClB,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW,GAAG,gBAAY,IAAI,QAAe;AAAA,EAC7C,aAAa;AACf,CAAC;;;ADFM,IAAM,sBAAsB,CAAC,YAAiC;AAC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;AACjG,QAAM,UAAU;AAEhB,MAAI,CAAC,QAAQ,MAAM;AACjB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,SAAO,8BAA8B,EAAE,YAAY,QAAQ,MAAM,cAAc,SAAS,aAAa,CAAC;AACxG;","names":[]}
package/dist/types.d.mts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from '@clerk/shared/types';
2
- export { C as ClerkFastifyOptions } from './index-C5pGnL7a.mjs';
2
+ export { C as ClerkFastifyOptions } from './index-CWnSaB0B.mjs';
3
3
  import '@clerk/backend';
4
4
  import '@clerk/shared/proxy';
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from '@clerk/shared/types';
2
- export { C as ClerkFastifyOptions } from './index-C5pGnL7a.js';
2
+ export { C as ClerkFastifyOptions } from './index-CWnSaB0B.js';
3
3
  import '@clerk/backend';
4
4
  import '@clerk/shared/proxy';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerk/fastify",
3
- "version": "3.1.27-snapshot.v20260515233846",
3
+ "version": "3.1.27",
4
4
  "description": "Clerk SDK for Fastify",
5
5
  "keywords": [
6
6
  "auth",
@@ -62,8 +62,8 @@
62
62
  "dependencies": {
63
63
  "cookies": "0.9.1",
64
64
  "fastify-plugin": "^5.1.0",
65
- "@clerk/backend": "3.4.9-snapshot.v20260515233846",
66
- "@clerk/shared": "4.12.0-snapshot.v20260515233846"
65
+ "@clerk/backend": "^3.4.9",
66
+ "@clerk/shared": "^4.12.0"
67
67
  },
68
68
  "devDependencies": {
69
69
  "fastify": "^5.8.5"